Anda di halaman 1dari 7

C

History
The language B was developed in 1969-70 by Ken Thompson derived directly
from Martin Richards BCPL. B, a high-level language was used for
development of the UNIX system with few lines of code. But, B doesnt
support data-types and structures.
In 1971-73 Dennis M. Ritchie turned the B language into the C language, by
using the syntax of B language and adding few features to it. C was used to
program an operating system, especially Unix Kernel was written in C. C is
known for its features like Reliability, Portability, Flexibility, Interactivity,
Modularity, Efficiency and Effectiveness.
Why C, a middle level language??
It combines the elements of high-level languages with the
functionalism of assembly language.
It supports Inline Assembly Language Programs by which system
registers can be accessed.
It is used to access memory using pointers and is more user
friendly.
Why C, a structured oriented programming??
To solve a large problem, C programming language divides the problem into
smaller modules called functions or procedures each of which handles a
particular responsibility.
Definitions
1. Data Types: It is used for declaring variables or functions of different
types.
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char 1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
-32,768 to 32,767

unsigned short 2 bytes


long
4 bytes
unsigned long 4 bytes

0 to 65,535
-2,147,483,648 to 2,147,483,647
0 to 4,294,967,295

The basic datatypes are,


Int
Float
Char
Double
These datatypes have some modifiers as,
Short
Long
Signed
Unsigned
sizeof is the operator used to find the size of an variable. For example,
int x;
sizeof(x) would yield the output 4.
2. Variables:
It is a named area of storage that tells the compiler where and how much the
storage should be. In Turbo C, it accepts variable size upto 32 characters
,whereas gcc and visual studio accepts characters upto 2048 characters. The
first character of a variable must be an alphabet.
3. Types of Variables:
Local Variables: These variables have scope within a block or function, to be
declared on the top of the block and user has to initialize it.
Global Variables: These variables are visible to any function, to be declared on
the top of the function and it gets initialized automatically.
int
0
char
\0
float
0
pointer
NULL
If there is a conflict between local variable and global variable, then the local
variable gets the higher priority. For example,
?
#include<stdio.h>

int x=100
int main()
{
int x=50;
printf("%d",x);
}

Here the output is 50.


4. Tokens: These are the basic building blocks to write a C program.
Types:
Keywords
Identifiers
Constants
Strings
Operators
Special Symbols
5. Constants: They are variables with fixed values, also called as Literals.
Types:
Integer constants
Real or Floating point constants
Octal & Hexadecimal constants
Character constants
String constants
Backslash character constants
6. Keywords:
These are reserved by which their meaning is pre-defined. C language
supports 32 keywords and these keywords cannot be used as Identifiers.
Some reserved words are,
auto

else

long

switch

break

enum

register

typedef

case

extern

return

union

char

float

short

unsigned

const

for

signed

void

continue

goto

sizeof

volatile

default

if

static

while

do

int

struct

_Packed

double
7. Identifiers: Names given for identifying variables, functions and arrays are
termed as Identifiers.
8. Operators: These are the symbols used for performing logical and
mathematical operations. Operators join individual constants and variables to
form an expression. C language provides several operators like,
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Increment/decrement operators
Special operators
9. Strings: These are array of characters represented using double quotes
and ends with null (\0).
10. Arrays: Arrays are collection of variables of same datatype having a
contiguous memory allocation.
Types:
One dimensional array
Multi dimensional array

Two dimensional array

Three dimensional array, four dimensional array etc

11. You can also represent char and int in hexadecimal and octal notations.
hex num is preceded by 0x or 0X, For octal, its preceded by 0 (zero). you
cant represent a float num as hexa and octal.
12. Basic precedence order : unary minus has the highest priority and then
the order is *,/,%,+,- and then assignment operators.
13. scanf( ) and printf ( ) :
These are the standard library functions which is used to get the input from
the user and print the output on the screen.
Eg : printf( enter your age);

scanf( %d,&n);
In scanf the variable name must be preceded by address of operator (&).
14. printf( ) and sprintf ( ) :
printf( ) is used to display the ouput on screen whereas sprintf( ) is used to
store the output to an array.
15 Can you guess the output of this code :
?
#include <stdio.h>
int main(void) {
printf("size of '5' is %d \n", sizeof('5'));
printf("size of 5 is %d \n", sizeof(5));
printf("size of 5.0 is %d \n", sizeof(5.0));
return 0;
}

If you guess the output as 4,4,8 means you are right


Explanation :
sizeof(5) means, it takes the ASCII value of character 5 is 53, hence the
sizeof(53) is 4
sizeof(5) is 4
sizeof(5.0) here 5.0 , floating point constant which always stored as a
double, which takes 8 bytes.
( If it declared as a float variable, then it would yield 4 bytes)
For ex :
float a=5.0;
printf(%d,sizeof(a)) would yield 4 bytes.
16. Can you guess the output of this code
?
#include <stdio.h>
int main(void) {
printf("%d\t",5%3);
printf("%d\t",5%-3);
printf("%d\t",-5%3);
printf("%d\t",-5%-3);
return 0;
}

The output is 2 2 -2 -2

% operator returns the reminder on dividing the first integer by the second. If
any one of them is negative,then the result takes the sign of the numerator.
17. What is the output of the following code?
?
#include <stdio.h>
int main(void) {
float a=7,b=2;
int c;
c=a%b;
printf("%d",c);
return 0;
}

Here the output is not 3 : (. Surprised? The output is error, because %


operator does not work on float.
18. Guess the output :
?
#include <stdio.h>
int main(void) {
int x;
x= -5 * -11 % -7/-3;
printf("%d",x);
return 0;
}

Here the output is -2. ( The precedence order is *,%,/)


19. Guess the output :
?
#include <stdio.h>
int main(void) {
int c=50000*50000/50000;
printf("%d",c);
return 0;
}

Here the output is definitely not 50000. Surprised? Here the value of
50000*50000 exceeds the range of an integer.
20. Wed recommend you to use visual studio or gcc compiler. Otherwise use
online compiler indeone or codepad.org.
Try these simple programs.
1. Write a program to compute the seconds from the given age.
2.Write a program to compute simple interest and compound interest from the
given principal amount,interest rate and time period.

3. Write a program to swap two variables without using third variable.


Hope you learn something new or it may help you to refresh the old concepts.
Our next article is about if-else statement,switch statement and conditional
operator.
This article was written by Swathi U V and Arun Prakash M.
Thank you.

Anda mungkin juga menyukai