You are on page 1of 37

C Programming

Data Types
• Why Datatypes ? • Data come in different forms integers(1,10,100),decimals(1.3,99.999),character s(@,%,$) • Different are required In order to inform compiler to allocate memory for a variable. • char -1 byte, int -2 bytes, float -4 bytes . • Datatypes: char, int, float, unsigned int,signed int,long int,double

Integer variable
• Datatype represented as int • An int type variable consumes 2 bytes of memory. • int a=25; • a is stored as 0000 0000 0010 0101 in the memory – denotes 2 bytes memory consumption.

• Basically used to store decimal numbers .89.Float variable • Datatype represented as float • A float type variable consumes 4 bytes of memory. • Float a=25.

• ASCII value of @ is 64 • Correspondingly c is stored in memory as 0110 0100 – denotes 1 byte memory consumption . • char c=‘@’ .Character variable • Represented as char • A character variable consumes 1 byte of memory.

putchar() are few i/o built in functions. • scanf(). . • printf() function is used to write the output into the monitor/console.Input Output functions • C defines few Input/Output functions in stdio.printf().getchar().h file. • scanf() function is used to read the input from keyboard.

scanf(“%d%d”.&a.a. .b). putchar(d).&b). printf(“a=%d b=%d”. char d=‘$’.b.• Usage: int a.

&&.logical.relational. • Example . . +.incre ment/decrement.-.assignment.*.|| etc… • Different categories of operators are: arithmetic..Operators • Operator is a symbol that tells computer to perform some mathematical or logical operations./ .

y. multiplication and division respectively. y=a/b.b=30. / are the arithmetic operators to perform addition. subtraction. -. • Usage: int a=25. y=a*b. *. . y=a+b.Arithmetic Operators • +.

==. >=. . • All the operators return true or false as the result of comparison. • >. <.Relational Operators • Used to make comparison between different data elements. <=. != are some of the relational operators used for comparing different variables.

b=23. } If(a!=b) { Play football. } .Usage: int a=25. If(b>a) { play cricket.

• These operators are used to perform logical manipulations. . not logics respectively. or . • All the operators boil down to true or false after the operation is complete.Logical operators • && . ! are the logical operators for and . || .

} If(!(a==b)||(a<b)) { printf(“let us C”). if((a>b)&&(a==25)) { printf(“Gone in 60 seconds”).• Usage: int a=25. } .b=100.

• = is the assignment operator.y. • The left hand side of = must be a variable and right hand side must be a value. y=a*a/2 . • Usage: int a=10.Assignment Operator • Used to assign some value to the variable.


IF/ELSE/ELSE IF • If /else statements are used for making simple decisions in C programming. • There can be a simple if statement or a if followed by else • For multiple conditions else if statements can be used. • The if statement has a boolean expression returning true or false. .

} else { Action 2. } .• Usage: ef(a>b) { Action 1.

} else if(a!=0) { Action2.If(a>b) { Action 1. } else { Action 3. } .


• The looping continues until the condition mentioned is true.While loop • Used to iterate/loop over the block of code through a condition. • Can be thought as indeterminate loop. • Control from the Loop exits when the condition mentioned becomes false. .

Usage: while(a>b) { printf(“inside a while loop”). . } • the printf() executes until a>b is true.

• The looping continues until the condition mentioned is do statement true.Do while loop • Used to iterate/loop over the block of code through a condition in do statement. • The block of code inside do executes atleast once even if the condition is false. .

do { ++a. } while(a!=b) The loop runs until a!=b becomes false. .

• Can be thought as a determinate loop. • For loop contains variables to control the number of iterations. . • Control from the loop exits when the mentioned number in loop is matched.For loop • For loop is used when we want to iterate through for definite number of times.

for(i=0. i<100 etc . The iterations can be determined by setting a limit to i value like i<10.i<10.i++) { printf(“inside for loop”). } The printf statement executes for 10 times.


Case statements • Switching mechanism in C is used to switch between different cases data elements. • There cannot be duplicate cases. . • The cases can be for integer and character elements. • Its recommended to place break statements after each block in a case .

break. a=2. case2: : printf(“case 2”). default:printf(“case default”). break. switch(a) { case 0: printf(“case 0”). } .

Arrays .

3. declaration and assigning of values can happen at compile time.67. • int a[10]. • 10 is size of array and should be hard coded in the declaration. • Size of an array should be a constant value.Single dimension arrays • An array is a collection of elements of same data type.10.8}. • int b[3]={5.10} is illegal. Cannot have float elements for int declaration as stated in definition . • int c[3]={1.4.

.4.{6.7.2 dimensional arrays • The array elements are written in column and row format Ex: int array_demo[2][3]={{3.7}.9}}. • The above array contains 2 rows 3 columns.

Strings .

.strings • A String is a collection of characters. • A ‘\0’ character is inserted in the before the string ends to indicate the end of characters. • “max%” == . ’a’. ’x’ . • char datatype is used to represent the string as there is no built in datatype for strings in C • char name*10+=“maximum”.‘m’.’%’-.

str2).str2) –concatenates str2 with str1 • strcpy( compare 2 strings • strlen(str).to calculate length of string • strcat(str1.str2)-copy str2 to str1 .String manipulation functions • C provides many predefined functions to manipulate the string elements. • strcmp(str1.

Pointers • A pointer is a variable that points to other variable by holding the address of variable it is pointing to. say &a equals 1000 (memory address) • Now p equals 1000 and *p equals 100 . • a is a variable. int *p= &a. p is the pointer pointing to a • &a is the address of memory location where a lives. • int a=100.

y= *p+100. • now int a=25. int *p=&a. now value of a is 99 . • A float pointer cannot point to a integer variable. • One can perform different manipulations using pointers. float *p=&a this is illegal. int y=0.• int a =25. now value of y is 125 *p=99.

}. float price.Structures • Is an array of elements of different datatypes struct book_bank { char title[20]. int pages. .