You are on page 1of 16

C PROGRAM

structure of C:

#include<stdio.h> //preprocessor directive


global varialbe;
int main() //main method of the program
{ /* main
block
return 0;
} */

Datatypes:

Requirements:
 Memory allocation (reserving space )
 restrict the access of the operation.
Different sizes of datatypes
1. Char:(1byte)
character datatype

2. int: (2 bytes (16bit), 4bytes (32 bit))


integer datatype

3. float:(4 bytes)
decimal type datatype

4. double:(8 bytes)
decimal datatype with more precision than float.

5. Void :
Datatype with no return value.

Note:
 Addresses of all the datatype for a 16 bit computer is 2 byte, in 32 bit computer is 4 byte.
 Initialisation is giving the value to the variable.
 re-initisialisation is over-writing the value to the same variable.
 Memory cannot be deleted , but only can be over-written.
 int a=10; //is called definition as declaring and initialisation has been done at the same time

Identifiers:
Name given to memory location.

Rules:
i. Keywords cannot be used as identifiers (there are 44 keywords in C)
ii. An identifier cannot start with numbers, special symbols ; other than underscore („_‟),
alphabets and can have alpha-numerals in them.
iii. Identifier can have only 32 characters.

1
Variables:
variables are identifiers.
They are the name given to the initial memory address. Values can be changed any number of times
during execution.
 Local variables has to be initialised before accessing because it will have junk values.

 Global variables will have default value zero „0‟ in them , hence they can be used without
initialisation. Exceptional case char will have \u0000 which indicates blank/spaces.

 ASCII value of A=65in decimal……Z=90 in decimal, a=97in decimal……z=122 in


decimal. Characters are stored in the form of ASCII values

 Example: char ch=‟L‟ will have the following in it.

27=128 26=64 25=32 24=16 23=8 22=4 21=2 20=1


- 1 0 0 1 1 0 0
Whose ASCII value is 76.
 The global variables can be accessed using scope resolution operator (::) & pointers.

INPUTS OUTPUTS
Scanf Printf
Getch Putch
Gets Puts

Format specifiers:

FORMAT SPECIFIER FUNCTION


%d Integer
%c Character
%u Unsigned integer
%f Float
%lf Long float/double
%Lf Long double
%x Hexadecimal
%o Octal
%i Integer/hexa/octa
%E %e Exponential
%p Address
%g Foat/double
%s String

2
OPERATORS:
1. Arithmetic operators:
a. Unary operators:
++, --.
b. Binary operators:
+,-,*,/,%
c. ternary operator:
(condition)?statement1: statement2;

/* if condition is true , statement1 will be executed else statement2 will be executed */

2. Relational operators:
These operators return the value 1 or true when the condition is satisfied and return 0 or
false when the condition is not.
<, >, ==, <=, >=
3. Logical operators:
&&( logical AND), || ( logical OR), ! (logical NOT)
& (bitwise AND), | (bitwise OR), ~ (bitwise NOT)
4. Assignment operators:
+=, -=, *=, /=, %=,……
5. Increment and Decrement operators:
if a is a variable declared then

Operator Meaning
a++ Post increment (use and increment by 1)
++a Pre increment (increment by 1 and use it)
--a Pre decrement (decrement by 1 and use it)
a-- Post decrement (use and decrement by 1)

6. Left shift ( ‘<<’ )t and right shift operator( ‘>>’):


These operators will shift the values to either left (left shift operator ) or right (right shit
operator)

Syntax for
right shift operator
Variable>>n; //value of variable shifts right by n times
Left shift operator
Variable<<n; // value of variable shifts right by n times

3
Example : left shfit operator (shifting value in a to the left by 1)
a<<1;

Initial 27 26 25 24 23 22 21 20
value 1 0 0 0 1 0 1 1

During 27 26 25 24 23 22 21 20
left shift
1 0 0 0 1 0 0 1
After 27 26 25 24 23 22 21 20
left
0 1 0 0 0 1 0 0
shift

we can see after left shift operation, the value in LSB is LOST and the MSB value will be 0.
The operation of right shift operator will be the same but in opposite direction

Precedence :
Precedence level for different operator in highest precedence to lowest is as follows
Unary operator.
Binary operator.
Ternary operator.
Assignment operator.
The detailed information of precedence and their order of associativity is as shown in fig 1.

Remainder table:
the remainder of some numbers when the following numbers are divided by some number is

2 3 4 5 n
0 0 0 0 0
1 1 1 1 1
2 2 2 2
3 3 …
4 …
n-1

/*program to swap 2 no‟s Logic for swaping of numbers


without 3rd variable*/ without extra variable.
1. add all the numbers
#include<stdio.h> eg a=a+b+c+d+…
2. take (n-1t)thvariable and
#include<conio.h>
subtract the number from
int main()
the sum.
{ Eg b=a-(b+c+d+…)
int a=5, b=10; 3. continue the same for rest
a=a+b; of the variable.
b=a-b; The sample program is given
a=a-b; on the left side othis box
}

4
Fig 1 . table Associativity of
showing printf(); is
precedence from right to
order and their left.
laws of
associativity

5
Control statements:
1. Conditional statements
i. If statements:
Syntax is

if (condition)
{
Statements;
}
else
{
Statements;
}

ii. Nested if:


Syntax is

if(condition)
{
if(condition)
{
Statements;
}
}
else
{
Statements;
}

#include<stdio.h>
#include<conio.h>
int main() printf (); statement will
{ always return the
int ans=0; number of characters
if(ans=printf(“mibweb”))
{
printf(“\n mib %d”,ans);
}
else
{
printf(“\n web %d”,ans);
}
getch();
clrscr(); Output:
mibweb
}
mib 6

6
iii. Switch statements:
Syntax is

Switch(condition) Switch statements only


{ executes the staments
Case 1: which are inside the case
statements; only.
break; The statements outside the
Case 2: case are ignored.
Statements;
break;
Case 3:
Statements;
break;
……
Case n:
Statements;
break;
Default:
Default statements;
break;
}

2. Looping statements:
i. For loop:
Syntax is

for(initialisation;condition;updation
)
{
Statements;
}
ii. While loop;
Syntax is

while (condition)
{
Statements;
}

iii.Do-while loop:

syntax is

Do Do-while loop execute


{ atleast for once even if the
Statements; condition is false
}
While(condition);

7
Loop inside a loop is
Loop termination: called nested loop.
i. break: this statement will terminate and come out of the loop
ii. continue: this statement will go back and continue the loop after some condition is satisfied.
iii. goto: this statement will jump the label assigned.

value of columns j
Value 00 01 02 03 04
of 10 11 12 13 14
rows 20 21 22 23 24
i 30 31 32 33 34
40 41 42 43 44
Values in i_j format

Value of columns j
Value 0 1 2 3 4
of 1 2 3 4 5
rows 2 3 4 5 6
i
3 4 5 6 7
4 5 6 7 8
Values in i + j format

Value of columns j
Value 0 -1 -2 -3 -4
of 1 0 -1 -2 -3
rows 2 1 0 -1 -2
i 3 2 3 0 -1
4 3 2 1 0
Values in i -j format

Value of columns j
Value 0 0 0 0 0
of 0 1 2 3 4
rows
0 2 4 6 8
i
0 3 6 9 12
0 4 8 12 16
values in i*j format

MACRO’s:
i. #define directive:
This is a pre processor directive where it will be loaded into program before executing the program.
Example: #define pi 3.14 /* the pi is replaced by 3.14 throughout the program*/

8
#include<stdio.h>
During execution of the
#include<conio.h>
program abc is replaced by
#define abc 2+5+6+9
2+5+6+9
int main()
{
sum=4*2+5+6+9
int sum=4*abc;
Printf(“%d”, sum);
This operation will be done as
Getch();
per precedence and laws of
Clrscr();
associativity. Hence the
}
output will be as shown.
Output:
sum=28

Functions:
Syntax is

Return type (parameters)


{
Statements;
return ;

Types of functions
1. Without return type and without arguments
2. With return type and without arguments
TurboC++ is has 16bit
3. Without return type with arguments
architecture
4. With return type and arguments
5. Recursive function. (function calling itself).

Parts of function
i. Function definition
ii. Function declaration
iii. Function call (call by value and call by reference).

Default return type is integer for


turboC++

9
#include<stdio.h>
#include<conio.h>
void pascal abc(int a, int b, int c, intd);
void _cdecl mno(int a, int b, int c, int d)
{
printf(“_cdecl: %d \t %d \t %d
\t %d\t”,a,b,c,d);
For precedence }
int main()
1. Pascal is left to {
right. int a=5
2. cdecl is right to mno(a++,--a,a++,a);
left int a=5;
3. default it is abc(++a, --a,a++,a);
_cdecl Output: getch();
_cdecl: 6 5 5 5 clrscr();
also pascal and Pascal: 6 5 5 6 }
_cdecl are key words
void pascal abc(int a, int b, int c ,int d)
{
printf(“pascal: %d \t %d \t %d \t %
d”,a,b,c,d);
}

Pointers: Two &‟s cannot come


Pointer is a variable which stores the address of another variable. together in a pointer.
&(address of) :
Gives the address of the variable, example : &a //displays the address of a „ && ‟ gives an error in
*(pointer to ): the program.
Gives the value pointing to the address of the variable.
Pointers are used for dynamic memory allocation.
Example: *p //display the value present in the address pointing to p.

If addressof a=1000, address of a=10


p=2000, address of q=3000 as
shown in the box to the left. a 1000

int a=10;
int *p=&a; 1000
int **q=&p;
q // 2000 p 2000
&q // &(2000)=3000
*q // *(2000) i.e.,=1000
2000
**q // *(1000)=10
q 3000
10
Arrays :
It is a sequential memory allocation which can store homogenous data .
int a[i] ; when decoded gives the following. If int a[i];
o [ ] indicates the number of blocks. a is same as a[0] .
o a indicated initial address. *(a+i) is same as a[i].
o i indicates the index.
scanf(“%d”,a+i);
a[i]= address of a+(size of datatype *i).
The last content of the array is \0. scanf(“%d”,&a[i]);
Arrays are used to optimise the memory allocation.

Program displaying address,


index and value stored.

11
Multidimensional array: The memory allocated will be
((size of datatype*j)*i)
Graphical representation of a[i][j] say i=4, j=3.
This can also be represented as a pointer as *(*(a+i)+j)
In three dimensional array, the extra blocks are created inside j and it continues to build blocks
inside the blocks as the dimension of the array increases.

j 0 1 2 0 1 2 0 1 3 0 1 2

i 0 1 2 3

The following program makes use of different ways to access an array.

12
13
Structures and unions:
Structure is a collection of different datatypes. Using structures one can create his own data types.
The members of the structures can be accessed using the dot ( . ).

Syntax are

i. struct identifier ii. typedef struct identifier


{ { typedef is used to rename
declaration; a structure. This is called
declarations; } new_identifier; aliasing

};

Structure inside a structure is nested structure.


Structure variable cannot be incremented or decremented
The variables present inside the structure can be accessed using ( -> )operator.

The memory bits can be reduced by using ( : ) operator.


Example: int a:5; //the size of a is restricted to 5 bits

14
Unions:
Unions are same as structures the only difference is memory allocated for the highest size of
datatype present and the same block is divided for other variable present , hence the name union .
only one value can be inserted at a time .

Example:
union sample
{
int a ;
float b;
char c;
};

For the given piece of code , the memory allocated for float as it is the highest datatype present in
the block, and the rest datatype present inside are accomodated inside the float.

Memory allocation :
Malloc(): used to allocate memory for local variables and has garbage or junk values.
Void *malloc(number of blocks*sizeof datatype)
Both malloc()
and calloc() has
Calloc(): used to allocate memory for global variables and has default variable. the return type of
Void * calloc (number of block , sizeof datatype). void.
Both are used for dynamic memory allocation.

Realloc() : used to re allocate the size of memory. It can increase as well as decrease the size of the
memory.

Free() : will de reference the pointer variable storing the values of caalloc() and/or malloc().

These functions are present in stdlib.h .

15
Line no. program
#include<stdio.h>
#include<conio.h>
int i;
void abc(int i); //replace i by *i for call by reference
1. int main()
2. {
3. printf(“main starts \n”);
4. abc(i); //for call by reference replace i by &i
5. printf(“main ends\n”);
6. getch();
7. clrscr();
8. }
9. void abc(int i;) // for call by reference replace i by *i
10. {
11. printf(“%d\”, i++); //for call by reference replace i++ by *i++
12. if(i<=5) //i is replaced by *i
13. abc(i+1); // i replaced by *i for call by reference
14. printf(“%d\n”,i);
15. }

Program flow during the execution:

16

You might also like