C - Complete Notes
C - Complete Notes
INTRODUCTION
In 1972 Ken Thomson & Denise Richie develop C Language in BELL Laboratories (USA).
English Binary
Ex:- C, Pascal, Cobal, Ex :- 0’s & 1’s
Basic, Fortran….. etc.
Character set: -
Special characters: -
A–Z # $ ! & ^ ¦ * / % + - { } ( ) [ ] : ; . , ‘ “ \ = > < ? ……. etc
a–z
0–9
Operators: -
1
Data Types: -
Alt + F9 compile
Ctrl + F9 run
Alt + F5 see the output
2
5. Simple Program about scanf
#include<stdio.h>
void main( )
{ x Output:
int x; 12
clrscr( ); enter no: 12
printf(“enter no:”); x :12
scanf(“%d”,&x);
printf(“x :%d”,x);
gech( );
}
3
9. Write a program to print area of circle. (a=r2)
#include<stdio.h>
#include<conio.h> r a Output:
void main( ) 1
{ enter r :1
int r; a : 3.140000
float a;
clrscr( );
printf(“enter r :”);
scanf(“%d”,&r);
a = 3.14 * r * r;
printf(“a :%f”,a);
getch( );
}
10. Write a program swapping or interchange two numbers. (using third variable)
void main( )
{
int x,y,t; x y Output:
clrscr( ); 10 20
printf(“enter two nos :”); enter two nos: 10 20
scanf(“%d%d”,&x,&y); t x :10 y : 20
printf(“x :%d y :%d \n”,x,y); x :20 y : 10
t = x;
x = y;
y = t;
11. Write a program swapping or interchange two numbers. (without using third variable)
void main( )
{
int x,y; Output:
clrscr( ); x y
printf(“enter two nos :”); 10 20 enter two nos: 10 20
scanf(“%d%d”,&x,&y); x : 10 y : 20
printf(“x :%d y :%d \n”,x,y); x : 20 y : 10
x = x + y;
y = x - y;
x = x - y;
printf(“x :%d y :%d \n”,x,y);
getch( );
}
4
12. Write a program to add, sub, multi, divide & find remainder of the two nos.
void main( )
{
int x,y,a,b,c,e; Output:
float d; x y
clrscr( ); 10 3 enter two nos: 10 3
printf(“enter two nos:”); a : 13 b : 3 c : 30
scanf(“%d%d”,&x,&y); d : 3.333333 e : 1
a = x + y;
b = x – y; a b c d e
c = x * y;
d = x / (float) y;
e = x % y;
printf(“a :%d b : %d c : %d \n”,a,b,c);
printf(“d :%f e : %d ”,d,e);
getch( );
}
5
15. Pre-decrement & Post-decrement
void main( )
{ Output:
int x = 10; x
clrscr( ); 10 x :10
printf(“x :%d\n”,x); x :10
printf(“x :%d\n”,x--); x :9
printf(“x :%d\n”,x); x :8
printf(“x :%d\n”,--x); x :8
printf(“x :%d\n”,x);
getch( );
}
6
LOOPS
Def: -
One or more that one statement(s) are repeated or executed through a condition.
Types of loops: -
1. If
2. While
3. For
4. Switch
If: -
Def: -
Check the condition, when condition is true execute if following statement(s).
Syntax: -
if(cond) if(cond)
statement1; (or) {
B.S.
}
7
If-else: -
Def: -
Check the condition, when condition is true execute if following statement(s). When condition is
false else following statement(s) will be executed.
Syntax: -
if(cond) if(cond)
statement1; (or) {
else
statement2; B.S.
}
else
{
B.S.
}
8
3. Write a program to print greatest of two nos. (using if-else)
void main( ) x y
{ 20 10
int x,y;
clrscr( );
printf(“enter two nos:”); Output:
scanf(“%d%d”,&x,&y);
if ( x > y) enter two nos: 20 10
printf(“%d”, x); 20
else
printf(“%d”, y); enter two nos: 10 50
getch( ); 50
}
4. Write a program to check the given two nos are equal or not equal. (using if-else)
void main( ) x y
{ 10 10
int x,y;
clrscr( );
printf(“enter two nos:”); Output:
scanf(“%d%d”,&x,&y);
if ( x = = y) enter two nos: 10 10
printf(“ equal ”); equal
else
printf(“not equal”); enter two nos: 10 5
getch( ); not equal
}
Nested if: -
If-else is following with another if-else.
Syntax: -
if(cond)
statement1;
else
if(cond)
statement2;
else
if(cond)
statement3;
else
if(cond)
:
:
:
9
1. Write a program to print greatest of three nos. (using nested - if)
void main( )
{ x y z
int x,y,z; 3 1
2
clrscr( );
printf(“enter three nos:”);
scanf(“%d%d%d”,&x,&y,&z); Output:
if ( x > y && x > z )
printf(“%d”, x); enter three nos: 3 2 1
else 3
if(y > z)
printf(“%d”, y); enter three nos: 2 3 1
else 3
printf(“%d”,z);
getch( );
}
2. Write a program to find total, average & grade of the student when three subjects are given.
void main( )
{
int m1,m2,m3,tot; Output:
float per;
clrscr( ); enter three sub marks: 80 80 80
printf(“enter three sub marks :”); m1 : 80
scanf(“%d%d%d”,&m1,&m2,&m3); m2 : 80
tot = m1 + m2 + m3; m3 : 80
per = tot / 3.0; tot : 240
printf(“ m1 : %d\n”,m1); per : 80.000000
printf(“ m2 : %d\n”,m2); distinction
printf(“ m3 : %d\n”,m3);
printf(“ tot : %d\n”,tot);
printf(“ per : %f\n”,per); m1 m2 m3
if( m1 >= 35 && m2 >= 35 && m3 >= 35 ) 80 80 80
{
if( per >= 75)
printf(“ distinction “); tot per
else
if( per >= 60)
printf(“ 1st class “);
else
if( per >= 50)
printf(“ 2nd class “);
else
printf(“ 3rd class “);
}
else
printf(“ fail “);
getch( );
}
10
While: -
Def: -
Check the condition, when condition is true execute the loop until the condition is false.
Syntax: -
while(cond) while(cond)
st1; (or) {
B.S.
}
11
4. Write a program to print 10, 9, 8, …….. 1
void main( ) i Output:
{ 10
int i=10; 10
clrscr( ); 9
while(i>=1) 8
{ :
printf(“%d\n”,i); :
i--; 1
}
getch( );
}
12
7. Write a program to check the given number is palindrome or not.
void main( )
{ num rem sum temp
int num, rem, sum=0,temp; 121 0 121
clrscr( );
printf(“enter the number :”);
scanf(“%d”,&num); Output:
temp=num;
while(num>0) enter the number : 121
{ 121 is palindrome
rem = num % 10;
sum = (sum * 10)+ rem;
num = num / 10;
}
if(sum = = temp)
printf(“%d is palindrome ”,temp);
else
printf(“%d is not palindrome ”,temp);
getch( );
}
13
9. Write a program to print sum of the digits of the given numbers.
void main( )
{ num rem sum
int num, rem, sum=0; 123 0
clrscr( );
printf(“enter the number :”);
scanf(“%d”,&num);
while(num>0) Output:
{
rem = num % 10; enter the number: 123
sum = sum + rem; 6
num = num / 10;
}
printf(“%d”,sum);
getch( );
}
For: -
Syntax: -
for(Exp1;Exp2;Exp3) (or) for(Exp1;Exp2;Exp3)
statement1; {
B.S.
}
Exp1: - (Initialization)
Ex: -
i=0; j=10; k=1; …….. etc.
Exp2: - (Condition)
Ex: -
i<=10; j>=1; k<=100; …….. etc.
14
2. Write a program to print 2, 4, 6, ……… 10
void main( ) i Output:
{ 2 2
int i; 4
clrscr( ); 6
for(i=2;i<=10;i=i+2) 8
printf(“%d \n”,i); 10
getch( );
}
3. Write a program to print 1, 3, 5, ……… 9
void main( ) Output:
{ i 1
int i; 1 3
clrscr( ); 5
for(i=1;i<=10;i=i+2) 7
printf(“%d \n”,i); 9
getch( );
}
15
7. Write a program to print 1, 2, 3, ……… n
void main( ) i n Output:
{ 1 5 enter n : 5
int i,n; 1
clrscr( ); 2
printf(“enter n :”); 3
scanf(“%d”,&n); 4
for(i=1;i<=n;i++) 5
printf(“%d \n”,i);
getch( );
}
8. Write a program to print 1+ 2 + 3+ ………+n
void main( ) Output:
{ i sum enter n : 10
int i,sum=0; 1 0 55
clrscr( );
printf(“enter n :”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum = sum + i;
printf(“%d \n”,sum);
getch( );
}
9. Write a program to print mathematical table. Output:
void main( ) i n
{ enter which table you want: 5
1 5
int i,n; 5* 1= 5
clrscr( ); 5 * 2 = 10
printf(“enter which table you want :”); 5 * 3 = 15
scanf(“%d”,&n); :
for(i=1;i<=10;i++) :
printf(“%2d * %2d = %2d \n”,n,i,n*i); 5 * 10 = 50
getch( );
}
10. Write a program to print
1
12
123
1234 line
12345
void main( ) 1 Output:
{ i n enter how many line you want : 5
int line,i,n; 1
1 5
clrscr( ); 2 2
printf(“enter how many line you want :”); 3 3 3
scanf(“%d”,&n); 4 4 4 4
for(line=1;line<=n;line++) 5 5 5 5 5
{
for(i=1;i<=line;i++)
printf(“%d \t”,i);
printf(“\n”);
getch( );
}
16
11. Write a program to print
1
22
333
4444
55555
void main( ) Output:
{
int line,i,j,n; enter how many line you want : 5
clrscr( );
printf(“enter how many line you want :”); 1
scanf(“%d”,&n); 2 2
for(line=1;line<=n;line++) line n 3 3 3
{ 1 5 4 4 4 4
for(i=1;i<=n-line;i++) 5 5 5 5 5
printf(“ ”); i j
for(j=1;j<=line;j++ ) 1 1
printf(“%2d”,line);
printf(“\n”);
getch( );
}
do-while: -
Def: - Execute the loop, after execution of loop check the condition when condition is true
execute the loop until the condition is false.
syntax: -
do
{
B.S.
}while(cond);
17
1. Write a program to print 1, 2, 3, ….. 10 (using do-while)
void main( )
{ i
int i=1; 1 Output:
clrscr( );
do 1
{ 2
printf(“%d\n”,i); 3
i++; :
} while(i<=10); :
getch( ); 10
}
switch: -
Select one statement from number of statements.
Syntax: -
switch (op)
{
‘char’
case (or) : statement 1; break;
const
‘char’
case (or) : statement 2; break;
const
‘char’
case (or) : statement 3; break;
const
:
:
default : statement n;
}
18
1. Simple program for switch.
void main( )
{ int n; n
clrscr( ); 3 Output:
printf(“enter no :”);
scanf(“%d”,&n); enter no : 3
switch(n) three
{
case 1 : printf(“ one ”); break;
case 2 : printf(“ two ”); break;
case 3 : printf(“ three ”); break;
case 4 : printf(“ four ”); break;
default : printf(“ aaaa ”);
}
getch( );
}
2. Simple program for switch.
void main( )
{ Output:
int n;
clrscr( ); n enter no : 3
printf(“enter no :”); 3 three four aaaa
scanf(“%d”,&n);
switch(n)
{
case 1 : printf(“ one ”);
case 2 : printf(“ two ”);
case 3 : printf(“ three ”);
case 4 : printf(“ four ”);
default : printf(“ aaaa ”);
}
getch( );
}
3. Simple program for switch.
void main( ) Output:
{ char x; x
clrscr( ); enter character : b
b
printf(“enter character :”); b
scanf(“%c”,&x);
switch(x)
{
case ‘a’ : printf(“ a ”); break;
case ‘b’ : printf(“ b ”); break;
case ‘c’ : printf(“ c ”); break;
case ‘d’ : printf(“ d ”); break;
default : printf(“ default statement ”);
}
getch( );
}
19
4. Simple program for switch.
void main( ) x
{ b Output:
char x;
clrscr( ); enter character : b
printf(“enter character :”); b c d default statement
scanf(“%c”,&x);
switch(x)
{
case ‘a’ : printf(“ a ”);
case ‘b’ : printf(“ b ”);
case ‘c’ : printf(“ c ”);
case ‘d’ : printf(“ d ”);
default : printf(“ default statement ”);
}
getch( );
}
5. Write a program for add, subtract, multiple, divide and find remainder of the two numbers when
operator is given.
void main( )
{
char op;
int x,y,a,b,c,e; float d;
clrscr( );
printf(“enter character [+ - * / %]:”);
scanf(“%c”,&op);
printf(“enter two nos :”);
scanf(“%d%d”,&x,&y);
switch(op)
{
case ‘+’ : a = x+y; printf(“ %d ”,a); break;
case ‘-’ : b = x-y; printf(“ %d ”,b); break;
case ‘*’ : c = x*y; printf(“ %d ”,c); break;
case ‘/’ : d = x/(float) y; printf(“ %f ”,d); break;
case ‘%’ : e= x%y; printf(“ %d ”,e); break;
default : printf(“ default statement ”);
}
getch( );
}
OUTPUT:
20
ARRAYS
Def: -
Declaration: -
i). Data type Variable[size]={values}; ii). Data type Variable[size];
int x[5]={10,20,30,40,50}; int x[3];
0 1 2 3 4 0 1 2
10 20 30 40 50
char s[10]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’}; char b[5];
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
a b c d e f g h i j
2. Write a program read five numbers store in array and print those five numbers.
0 1 2 3 4
void main( ) 10 20 30 40 50
{
int x[5],i;
clrscr( ); OUTPUT:
printf(“ enter 5 eles : ”);
for(i=0;i<=4;i++) enter 5 eles : 10 20 30 40 50
scanf(“%d”,&x[i]); eles : 10 20 30 40 50
printf(“eles :”);
for(i=0;i<=4;i++)
printf(“ %d \t”,x[i]);
getch( );
}
21
3. Write a program read five numbers store in array and print those five numbers in reverse order.
0 1 2 3 4
void main( ) 10 20 30 40 50
{
int x[5],i;
clrscr( ); OUTPUT:
printf(“ enter 5 eles : ”);
for(i=0;i<=4;i++) enter 5 eles : 10 20 30 40 50
scanf(“%d”,&x[i]); else : 50 40 30 20 10
printf(“eles :”);
for(i=4;i>=0;i--)
printf(“ %d \t”,x[i]);
getch( );
}
22
5. Write a program to find maximum and minimum number in given array.
0 1 2 3 4
void main( ) 10 5 3 1 6
{
int x[5], i, max, min;
clrscr( ); OUTPUT:
printf(“ enter 5 eles : ”);
for(i=0;i<=4;i++) enter 5 eles : 10 5 3 1 6
scanf(“%d”,&x[i]); max :10 min : 1
max=min=x[0];
for(i=1;i<=4;i++)
{
if(x[i]>max)
max=x[i];
if(x[i]<min)
min=x[i];
}
printf(“max :%d min :%d”,max,min);
getch( );
}
2D Array: -
Declaration: -
Data Type Variable[size][size];
Ex: -
int x[3][3];
float a[4][2];
char s[10][20];
23
2. Simple program for printing 3*3 matrices elements.
void main( ) OUTPUT:
{
int x[3][3],i,j; enter 9 eles : 1 2 3 4 5 6 7 8 9
clrscr( ); eles :
printf(“ enter 9 eles : ”); 1 2 3
for(i=0;i<=2;i++) 4 5 6
for(j=0;j<=2;j++) 7 8 9
scanf(“%d”,&x[i][j]);
printf(“eles : \n”);
for(i=0;i<=2;i++) 00 01 02 11 12 13
{ 11 12 13 21 22 23
for(j=0;j<=2;j++) 21 22 23 31 32 33
printf(“ %d \t”,x[i][j]);
printf(“\n”);
}
getch( );
}
OUTPUT:
3. Write a program to print transpose of the given matrices. enter 9 eles :1 2 3 4 5 6 7 8 9
void main( ) eles :
{ 1 2 3
int x[3][3],i,j; 4 5 6
clrscr( ); 7 8 9
printf(“ enter 9 eles : ”); transpose of the given matrices:
for(i=0;i<=2;i++) 1 4 7
for(j=0;j<=2;j++) 2 5 8
scanf(“%d”,&x[i][j]); 3 6 9
printf(“eles : \n”);
for(i=0;i<=2;i++) 00 01 02 1 2 3
{ 11 12 13 4 5 6
for(j=0;j<=2;j++) 21 22 23 7 8 9
printf(“ %d \t”,x[i][j]);
printf(“\n”);
}
printf(“\n transpose of the given matrices: \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“ %d \t”,x[j][i]);
printf(“\n”);
}
getch( );
}
24
4. Write a program to print trace of the given matrices.
void main( )
{
int x[3][3],i,j,sum=0;
clrscr( ); Output:
printf(“ enter 9 eles : ”); enter 9 eles : 1 2 3 4 5 6 7 8 9
for(i=0;i<=2;i++) eles: 0 1 2
for(j=0;j<=2;j++) 1 2 3 0 00 01 02
scanf(“%d”,&x[i][j]); 4 5 6 1 10 11 12
printf(“eles :”); 7 8 9 2 20 21 22
for(i=0;i<=2;i++)
{ trace of the given matrices:
for(j=0;j<=2;j++) 15 i j
printf(“ %d \t”,x[i][j]);
printf(“\n”);
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i = = j)
sum = sum + x[i][j];
}
}
printf(“\n trace of the given matrices: \n”,sum);
getch( );
}
25
printf(“ \n eles in y : \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“%d\t”,y[i][j]);
printf(“\n”);
}
printf(“ \n eles in z : \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“%d\t”,z[i][j]);
printf(“\n”);
}
getch( );
}
6. Write a program to multiple two matrices. (3*3) with (3*3)
void main( )
{
int x[3][3],y[3][3],z[3][3]i,j,k;
clrscr( ); Output:
printf(“ enter 9 eles in x : ”); enter 9 eles in x : 1 2 3 4 5 6 7 8 9
for(i=0;i<=2;i++) enter 9 eles in y : 1 1 1 1 1 1 1 1 1
for(j=0;j<=2;j++)
scanf(“%d”,&x[i][j]); eles in x : 0 1 2
printf(“enter 9 eles in y :”); 1 2 3 0 00 01 02
for(i=0;i<=2;i++) 4 5 6 1 10 11 12
for(j=0;j<=2;j++) 7 8 9 2 20 21 22
scanf(“%d”,&y[i][j]);
for(i=0;i<=2;i++) eles in y : 0 1 2
{ 1 0 0 0 00 01 02
for(j=0;j<=2;j++) 0 1 0 1 10 11 12
{ 0 0 1 2 20 21 22
z[i][j]=0;
for(k=0;k<=2;k++) eles in z :
z[i][j]= z[i][j] + (x[i][k] * y[k][j]); 1 2 3
} 4 5 6
} 7 8 9
printf(“ \n eles in x : \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“%d\t”,x[i][j]);
printf(“\n”);
}
printf(“ \n eles in y : \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“%d\t”,y[i][j]);
printf(“\n”);
}
26
printf(“ \n eles in z : \n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf(“%d\t”,z[i][j]);
printf(“\n”);
}
getch( );
}
27
FUNCTIONS
Function is sub program. It having own name and block of statements. When it is called from
main it will be executed.
main
function
function
function
Types of functions: -
28
2. Simple program on function.
void function1( );
void function2( );
void function1( )
{
printf(“\n we are in function1”);
function2( );
printf(“\n we are in function1”);
}
void function2( )
{
printf(“\n we are in function2”);
}
void area( )
{
int r; r a
float a; 1
printf(“enter r :”);
scanf(“%d”,&r);
a= 3.14 * r * r;
printf(“area of the circle :%f”,a);
}
29
4. Write a program to print area of the rectangle. (Using function)
30
void reverse( )
{
int num,rem,sum=0; num rem sum
printf(“enter no :”); 123 0
scanf(“%d”,&num);
while(num>0)
{
rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
printf(“reverse of the given no :%d”,sum);
}
7. Write a program to print area of the circle. (Using passing by argument & non-return type)
void main( ) Output :
{
void area( int r ); enter r : 1
int r; r area of the circle : 3.140000
clrscr( );
1
printf(“enter r :”);
scanf(“%d”,&r);
area( r ); actual parameter
getch( );
}
void area( int r ) formal parameter r a
{ 1
float a; local variable
a= 3.14 * r * r;
printf(“area of the circle :%f”,a);
}
8. Write a program to print area of the rectangle. (Using passing by argument & non-return type)
void main( ) Output :
{
void area( int l, int b ); enter l b : 10 2
int l,b; l b area of the rectangle : 20
clrscr( ); 10 2
printf(“enter l b :”);
scanf(“%d%d”,&l,&b);
area( l,b );
getch( );
}
void area(int l, int b ) l b a
{ 10 2
int a;
a= l * b;
printf(“area of the rectangle :%d”,a);
}
31
9. Write a program to swapping or interchanging of two numbers. (Using passing by argument &
non-return type)
void main( ) Output :
{
void swap( int x, int y ); enter two nos : 10 20
int a,b;
clrscr( ); a : 10 b : 20
printf(“enter two nos :”); a : 20 b : 10
scanf(“%d%d”,&a,&b);
printf(“ a : %d b : %d \n”,a,b); a b
swap( a,b); 10 20
getch( );
}
void swap( int x, int y )
{
x=x+y; x y
y=x-y; 10 20
x=x-y;
printf(“ a : %d b : %d \n”,x,y);
}
10. Write a program to print reverse of the given number. (Using passing by argument &
non-return type)
32
11. Write a program to print reverse of the given number. (Using passing by argument & return type)
12. Write a program to print area of the circle. (Using passing by argument & return type)
33
Recursion: -
Function calling it self.
return( f );
}
34
STRINGS
Declaration : -
ex:- ex:-
char x[5]=”gopi”; char x[10];
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
g o p i ‘\0’
1. strlen( )
2. strcpy( )
3. strcmp( )
4. strcat( ) <string.h>
Declaration: -
Declaration: -
36
5. Write a program to copy string one variable to other. [Using string function strcpy( )]
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12 19
void main( ) x
g o p i ‘\0’
{
char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12 19
printf(“enter string:”); y
scanf(“%s”,x);
strcpy(y,x); Output:
printf(“x :%s\n”,x); enter string : gopi
printf(“y :%s\n”,y); x : gopi
getch( ); y : gopi
}
37
strcat( ): - (string concatenation)
Use: - To combine to strings.
Ex : -
Declaration: - strcat (s1,s2);
strcat(s1,s2); strcat (x,y);
7. Write a program to combine two strings. [Using string function strcat( )]
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12 19
void main( ) x
g o p i ‘\0’
{
char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12 19
printf(“enter two string:”); y
k r i s h n a ‘\0’
scanf(“%s%s”,x,y);
printf(“x :%s\n”,x); Output:
printf(“y :%s\n”,y); enter two string : gopi krishna
strcat(x,y); x : gopi
printf(“x :%s\n”,x); y : krishna
printf(“y :%s\n”,y); x : gopikrishna
getch( ); y : krishna
}
strrev( ): - (string reverse)
Use: - To change the reverse of the given string.
ex: - strrev(s);
Declaration: - strrev(x);
strrev(string variable); strrev(a);
8. Write a program to print reverse of the given string. [Using string function strrev( )]
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12 19
void main( ) x
a b c d ‘\0’
{
char x[20]; Output:
clrscr( );
printf(“enter string :”); enter string : abcd
scanf(“%s”,x); string : abcd
printf(“ string :%s \n”,x); string : dcba
strrev(x);
printf(“ string :%s \n”,x);
getch( );
}
39
STRUCTURES & UNIONS
STRUCTURES
Def: -
Collection of different data types.
Declaration: -
}; }variable;
struct tag variable;
1. Write a program to print student details using structures. (i.e. rno, name & per)
struct student
{ rno name per
int rno;
char name[20]; s
float per;
2 20 4
};
void main( ) s
{ 2
struct student s; 6
clrscr( );
Output:
printf(“enter rno :”);
scanf(“%d”,&s.rno); enter rno : 1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); enter per : 70
printf(“enter per :”);
scanf(“%f”,&s.per); rno :1
name : gopi
printf(“\n rno :%d”,s.rno); per : 70.000000
printf(“\n name :%s”,s.name);
printf(“\n per :%f”,s.per);
getch( );
}
40
2. Write a program to print book details using structures. (i.e. bname, author & price)
struct book
{ bname author price
char bname[20];
char author[20]; b
20 20 4
float price;
};
void main( ) b
{ 4
struct book b; 4
clrscr( );
printf(“enter bname :”); Output:
scanf(“%s”, b.bname);
printf(“enter author :”); enter bname : c
scanf(“%s”,b.author); enter author : swamy
printf(“enter price :”); enter price :100
scanf(“%f”,&b.price);
printf(“\n bname :%s”,b.bname); bname : c
printf(“\n author :%s”,b.author); author : swamy
printf(“\n price :%f”,b.price); price : 100.000000
getch( );
}
3. Write a program to print purchasing item from super market using structures. (i.e. item, quantity
price & totalamt)
struct market
{ item quantity price totalamt
char item[20];
int quantity; m
20 2 4 4
float price, totalamt;
};
void main( ) m
{ 3
struct market m; 0
clrscr( );
printf(“enter item :”); Output:
scanf(“%s”, m.item);
printf(“enter quantity :”); enter item : pen
scanf(“%d”,&m.quantity); enter quantity : 5
printf(“enter price :”); enter price :10
scanf(“%f”,&m.price);
m.totalamt = m.quantity * m.price;
printf(“\n item :%s”,m.item); item : pen
printf(“\n quantity :%d”,m.quantity); quantity :5
printf(“\n price :%f”,m.price); price : 10.000000
printf(“\n totalamt :%f”,m.totalamt); totalamt : 50.000000
getch( );
41
}
4. Write a program to print details of employee using structures. (i.e. ename, desig & sal)
struct employee
{ ename desig sal
char ename[20];
char desig[20]; b
float sal;
20 20 4
};
void main( ) e
{ 4
struct employee e; 4
clrscr( );
printf(“enter ename :”); Output:
scanf(“%s”, e.ename);
printf(“enter desig :”); enter ename : ravi
scanf(“%s”,e.desig); enter desig : jr.asst
printf(“enter sal :”); enter sal :5000.00
scanf(“%f”,&e.sal);
printf(“\n ename :%s”,e.ename); ename : ravi
printf(“\n desig :%s”,e.desig); desig : jr.asst
printf(“\n sal :%f”,e.sal); sal : 5000.000000
getch( );
}
5. Write a program to print students details using array of structures. (i.e. rno, name & per)
struct student
{ rno name per
int rno; 0 26
char name[20]; s 1 26
float per; 2 26
}; 3 26
void main( ) 4 26
{ int i; s
struct student s[5]; 13
clrscr( ); Output: 0
for(i=0;i<=4;i++)
{ enter rno : 1
printf(“enter rno :”); enter name : gopi
scanf(“%d”,&s[i].rno); enter per : 70
printf(“enter name :”); :
scanf(“%s”,s[i].name); :
printf(“enter per :”); enter rno : 5
scanf(“%f”,&s[i].per); enter name : xyz
} enter per : 60
for(i=0;i<=4;i++)
{ rno :1
printf(“\n rno :%d”,s[i].rno); name : gopi
printf(“\n name :%s”,s[i].name); per : 70.000000
printf(“\n per :%f”,s[i].per); :
} rno :5
getch( ); name : xyz
} per : 60.000000
42
6. Write a program to print student details using passing an argument as a structures. (i.e. rno, name, m1, m2,
m3, total & per)
struct student
{ rno name m1 m2 m3 total per
int rno;
char name[20]; s
int m1,m2,m3,total;
float per; 2 20 2 2 2 2 4
};
void main( ) s
{ 3
struct student s; 4
void print(struct student s);
clrscr( ); Output:
printf(“enter rno :”);
scanf(“%d”,&s.rno); enter rno : 1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); enter three sub marks : 70 70 70
printf(“enter three sub marks :”);
scanf(“%d%d%d”,&s.m1,&s.m2,&s.m3); rno :1
print(s); name : gopi
getch( ); m1 : 70
} m2 : 70
m3 : 70
void print(struct student s) total : 210
{ per : 70.000000
s.total = s.m1 + s.m2 + s.m3;
s.per = s.total / 3.0;
printf(“\n rno :%d”,s.rno);
printf(“\n name :%s”,s.name);
printf(“\n m1 :%d”,s.m1);
printf(“\n m2 :%d”,s.m2);
printf(“\n m3 :%d”,s.m3);
printf(“\n total :%d”,s.total);
printf(“\n per :%f”,s.per);
}
7. Write a program to print student details using passing an argument as a structures & return type. (i.e. rno,
name, m1, m2, m3, total & per)
struct student
{ rno name m1 m2 m3 total per
int rno;
char name[20]; s
int m1,m2,m3,total;
float per; 2 20 2 2 2 2 4
};
43
void main( )
{
struct student s;
struct student print(struct student s);
clrscr( ); Output:
printf(“enter rno :”);
scanf(“%d”,&s.rno); enter rno : 1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); enter three sub marks : 70 70 70
printf(“enter three sub marks :”);
scanf(“%d%d%d”,&s.m1,&s,m2,&s.m3); rno :1
s = print(s); name : gopi
printf(“\n rno :%d”,s.rno); m1 : 70
printf(“\n name :%s”,s.name); m2 : 70
printf(“\n m1 :%d”,s.m1); m3 : 70
printf(“\n m2 :%d”,s.m2); total : 210
printf(“\n m3 :%d”,s.m3); per : 70.000000
printf(“\n total :%d”,s.total);
printf(“\n per :%f”,s.per);
getch( );
}
44
printf(“enter three sub marks :”);
for(i=0;i<=2;i++)
scanf(“%d”,&s.m[i]); rno :1
s.total = 0; name : gopi
for(i=0;i<=2;i++) m1 : 70
s.total = s.total + s.m[i]; m2 : 70
s.per = s.total / 3.0; m3 : 70
printf(“\n rno :%d”,s.rno); total : 210
printf(“\n name :%s”,s.name); per : 70.000000
for(i=0;i<=2;i++)
printf(“\n m[%d] :%d”,i,s.m[i]);
printf(“\n total :%f”,s.total);
printf(“\n per :%f”,s.per);
}
Nested Structure : -
Structure within structure.
Declaration: -
struct tag1 struct tag1
{ {
------
members of structures;
------
struct tag2
{ };
struct tag2
members of structures; {
members of structures;
} variable2;
} variable1;
struct tag1 variable1
}variable2;
getch( );
}
UNIONS
Def: -
Collection of different data types.
Declaration: -
}; }variable;
union tag variable;
46
1. Write a program to print student details using union. (i.e. rno, name & per)
union student
{
int rno;
char name[20]; s
float per;
20
};
void main( ) s
{ 2
union student s; 0
clrscr( );
Output:
printf(“enter rno :”);
scanf(“%d”,&s.rno); enter rno : 1
printf(“ rno :%d \n”,s.rno); rno :1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); name : gopi
printf(“\n name :%s \n”,s.name); enter per : 70
printf(“enter per :”); per : 70.000000
scanf(“%f”,&s.per);
printf(“\n per :%f”,s.per);
getch( );
}
47
POINTERS
Def: - Memory
Address specifier. 0
1
Declaration: - 2
Data type *variable; :
:
Ex: - x 10 1000
int *p; :
char *k; 1000 :
float *a; y 2000
2000 :
x y z z 4000
:
12 1000 2000
:
1000 2000 4000 65535
x : 10 y : 1000 z : 2000
&x : 1000 &y : 2000 &z : 4000
48
3. Simple program of pointer. 0 1 2 3 4
10 20 30 40 50
1000 1002 1004 1006 1008
void main( )
{
int x[5]={10,20,30,40,50};
clrscr( );
printf(“x[0] :%d &x[0] :%u \n”,x[0],&x[0]); Output:
printf(“x[1] :%d &x[1] :%u \n”,x[1],&x[1]); x[0] :10 &x[0] :1000
printf(“x[2] :%d &x[2] :%u \n”,x[2],&x[2]); x[1] :20 &x[1] :1002
printf(“x[3] :%d &x[3] :%u \n”,x[3],&x[3]); x[2] :30 &x[2] :1004
printf(“x[4] :%d &x[4] :%u \n”,x[4],&x[4]); x[3] :40 &x[3] :1006
getch( ); x[4] :50 &x[4] :1008
}
4. Write a program to print array of element using pointer.
0 1 2 3 4
10 20 30 40 50
1000 1002 1004 1006 1008
void main( )
{ p
int x[5]={10,20,30,40,50},*p,i;
clrscr( ); 1000
p=&x[0]; 2000
for(i=0;i<=4;i++)
{ Output:
printf(“%d \n”,*p); 10
p++; 20
} 30
getch( ); 40
} 50
5. Write a program to swap two numbers using passing by reference.
void main( ) Output:
{
void swap(int *x,int *y); enter two nos: 10 20
int a,b; a :10 b :20
clrscr( ); a :20 b :10
printf(“enter two nos:”);
scanf(“%d%d”,&a,&b); a b
printf(“a :%d b :%d \n”,a,b); 10 20
swap(&a,&b);
printf(“a :%d b :%d \n”,a,b); 1000 2000
getch( );
}
void swap(int *x,int *y) x y
{ 1000 2000
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
49
}
6. Write a program to find length of the given string. (using passing by reference)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
a b c d ‘\0’
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1001 1002 1003 1004
void main( )
{ Output:
void length(char *x);
char x[15]; enter string : abcd
clrscr( ); abcd
printf(“enter string :”); length : 4
scanf(“%s”,x);
length( x );
getch( );
}
void length(char *x)
{ x len
int len = 0;
1000 0
while( *x != ‘\0’ )
{
printf(“%c”,*x);
x++;
len++;
}
printf(“ length :%d”,len);
}
7. Write a program to print student details using pointers. (i.e. rno, name & per)
struct student
{ rno name per
int rno;
char name[20]; s
float per;
1000 1002 1022
};
void main( ) p
{
1000
struct student s,*p;
clrscr( );
printf(“enter rno :”); Output:
scanf(“%d”,&s.rno); enter rno : 1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); enter per : 70
printf(“enter per :”);
scanf(“%f”,&s.per); rno :1
p=&s; name : gopi
printf(“\n rno :%d”,prno); per : 70.000000
printf(“\n name :%s”, pname);
printf(“\n per :%f”, pper);
getch( );
50
}
8. Write a program to print student details using passing by reference as a structures. (i.e. rno, name, m1,
m2, m3, total & per)
struct student
{ rno name m1 m2 m3 total per
int rno;
char name[20]; s
int m1,m2,m3,total;
float per; 100 102 122 124 126 128 130
};
void main( )
{
struct student s;
void print(struct student *s);
clrscr( ); Output:
printf(“enter rno :”);
scanf(“%d”,&s.rno); enter rno : 1
printf(“enter name :”); enter name : gopi
scanf(“%s”,s.name); enter three sub marks : 70 70 70
printf(“enter three sub marks :”);
scanf(“%d%d%d”,&s.m1,&s,m2,&s.m3); rno :1
print(&s); name : gopi
printf(“\n rno :%d”,s.rno); m1 : 70
printf(“\n name :%s”,s.name); m2 : 70
printf(“\n m1 :%d”,s.m1); m3 : 70
printf(“\n m2 :%d”,s.m2); total : 210
printf(“\n m3 :%d”,s.m3); per : 70.000000
printf(“\n total :%d”,s.total);
printf(“\n per :%f”,s.per);
getch( );
}
51
FILES
Def: -
Declaration: -
FILE *p;
File functions: -
1. Write a program to create a file. or write information into the file.(using files)
#include<stdio.h> Output:
void main( ) enter text (at last press ctrl+z): abcdefgh^z
{
FILE *p; file1 “w” write
char c;
clrscr( ); p c
p = fopen(“file1”,”w”);
printf(“enter text (at last press ctrl + z) :”);
fclose(p);
}
52
3. Write a program to append or add information to the given file.(using files)
#include<stdio.h> Output:
void main( ) enter text (at last press ctrl+z): abcdefgh^z
{
FILE *p; file1 “a” appending
char c;
clrscr( ); p c
p = fopen(“file1”,”a”);
printf(“enter text (at last press ctrl + z) :”);
while ( ( c=getchar( ) )!=EOF)
putc( c, p );
fclose(p);
}
4. Write a program to copy one file information to other. (using fputc & fgetc)
#include<stdio.h>
void main( )
{
FILE *p1,*p2; file1
char c;
clrscr( ); p1 c
p1 = fopen(“file1”,”w”);
printf(“enter text (at last press ctrl + z) :”);
while ( ( c=getchar( ) )!=EOF)
putc( c, p1 ); p2
fclose(p1);
p1 = fopen(“file1”,”r”); /* copy */
p2 = fopen(“file2”,”w”); file2
while ( ( c=fgetc( p1 ) )!=EOF)
fputc( c, p2 );
fclose( p1 );
fclose( p2 );
53
5. Write a program to create file, send strings into the file and read strings from the file.
(using fputs & fgets)
#include<stdio.h> file1
void main( ) p x
{
FILE *p;
char x[20];
int i;
clrscr( );
p = fopen(“file1”,”w”); Output:
for(i=1;i<=5;i++)
{ enter string : aaaa
printf(“enter string :”); enter string : bbbb
scanf(“%s”,x); enter string : cccc
fputs( x , p ); enter string : dddd
fputc( ‘\n’, p); enter string : eeee
}
fclose(p); strings :
p = fopen(“file1”,”r”);
printf(“\n strings : \n”); aaaa
while( ( fgets( x, 20, p ) ) != EOF) bbbb
printf(“ %s \n”, x); cccc
fclose( p ); dddd
getch( );
}
6. Write a program to create a file, send numbers in to the file and read numbers from the file
(using putw & getw)
#include<stdio.h>
void main( )
{
FILE *p; file1 i
int i,x; p
clrscr( ); c
p = fopen(“file1”,”w”);
for(i=1; i<=10; i++)
putw( i , p );
fclose(p); x
p = fopen(“file1”,”r”); Output:
for(i=1; i<=10; i++) 1
{ 2
x=getw( p ); 3
printf(“%d \n”, x); 4
} :
fclose(p); 10
getch( );
}
54
7. Write a program to create a file, send student details (rno, name & per) in to the file and read
student details from the file. (using fprintf & fscanf)
#include<stdio.h>
void main( )
{
FILE *p; file1 rno
int rno; p
char name[20];
float per; name
clrscr( );
p = fopen(“file1”,”w”); per
printf(“enter rno :”); scanf(“%d”,&rno);
printf(“enter name :”); scanf(“%s”,name); Output:
printf(“enter per :”); scanf(“%f”,&per);
fprintf(p,”%d\t%s\t%f”,rno,name,per); enter rno : 1
fclose(p); enter name : gopi
p = fopen(“file1”,”r”); enter per :70
fscanf(p,”%d\t%s\t%f”,&rno,name,&per);
printf(“\n rno :%d”,rno); rno : 1
printf(“\n name :%s”,name); name : gopi
printf(“\n per :%f”,per); per : 70.000000
fclose(p);
getch( );
}
8. Write a program to create a file, send students details (rno, name & per) in to the file and read
student details from the file. (using fprintf & fscanf)
#include<stdio.h>
void main( )
{
FILE *p; file1 rno
int rno,i; p
char name[20];
float per; name
clrscr( );
p = fopen(“file1”,”w”); per
for(i=1;i<=5;i++)
{ Output:
printf(“enter rno :”); scanf(“%d”,&rno);
printf(“enter name :”); scanf(“%s”,name); enter rno : 1
printf(“enter per :”); scanf(“%f”,&per); enter name : aa
fprintf(p,”%d\t%s\t%f\n”,rno,name,per); enter per :70
} :
fclose(p); enter rno :5
p = fopen(“file1”,”r”); enter name :ee
while( fscanf(p,”%d\t%s\t%f\n”,&rno,name,&per)!=EOF) enter per : 50
{
printf(“\n rno :%d”,rno); rno : 1
printf(“\n name :%s”,name); name : aa
printf(“\n per :%f”,per); per : 70.000000
} :
fclose(p); rno : 5
getch( ); name : ee
} per :50.000000
55
1. Program on Local Variable.
int x = 5;
void main( ) OUTPUT:
{
void gopi( ); x :5
clrscr( ); x :5
printf(“x :%d\n”,x); x : 10
gopi( ); x : 10
printf(“x :%d\n”,x);
getch( );
}
void gopi( )
{
printf(“x :%d\n”,x);
x=x+5;
printf(“x :%d\n”,x);
}
56
3. Write a program add, subtract, multiply, divide and find reminder of two numbers when
operator is given ( + - * / %).
void main( )
{
void add(int x, int y);
void sub(int x, int y);
void mul(int x, int y);
void div(int x, int y);
void rem(int x, int y);
int a,b;
char op;
clrscr( );
printf(“enter op [+ - * / %] :”); scanf(“%c”,&op);
printf(“enter two nos:”); scanf(“%d%d”,&a,&b);
switch( op )
{
case ‘+’ : add(a,b); break;
case ‘-’ : sub(a,b); break;
case ‘*’ : mul(a,b); break;
case ‘/’ : div(a,b); break;
case ‘%’ : rem(a,b); break;
default :
57