C Language Complete Notes
C Language Complete Notes
INTRODUCTION
In 1972 Ken Thomson & Denise Ritchie 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);
getch( );
}
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( );
}
for(line=1;line<=n;line++) 5 5 5 5 5
16
{
for(i=1;i<=line;i++)
printf(“%d \t”,i);
printf(“\n”);
getch( );
}
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
Syntax: - while(cond) syntax: - do
{ { B.S
B.S.
} }while(cond);
When the condition is true then only execute the When the condition is true or false loop will be
loop. executed at least one’s.
switch: -
Select one statement from number of statements.
Syntax: -
switch (op)
{
‘char’
case (or) : statement 1; break;
const
18
‘char’
case (or) : statement 2; break;
const
‘char’
case (or) : statement 3; break;
const
:
:
default : statement n;
}
19
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( );
}
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( );
20
}
OUTPUT:
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
{
21
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( );
}
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
}
printf(“after sorting eles :\n”);
for(i=0;i<=4;i++)
printf(“ %d \t”,x[i]);
getch( );
}
2D Array: -
Declaration: -
Data Type Variable[size][size];
Ex: -
int x[3][3];
float a[4][2];
char s[10][20];
23
printf(“eles :\n”);
for(i=0;i<=1;i++) 00 01 11 12
{ 10 11 21 22
for(j=0;j<=1;j++)
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++)
24
printf(“ %d \t”,x[j][i]);
printf(“\n”);
}
getch( );
}
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”);
26
}
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”);
}
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: -
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( )
{
29
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);
}
30
void main( ) Output :
{
void reverse( ); enter no : 123
clrscr( ); reverse of the given no :321
reverse( );
getch( );
}
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);
31
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);
}
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
rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
printf(“reverse of the given no :%d”,sum);
}
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
getch( );
}
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’
35
3. Simple program on strings. 0 1 2 3 4 5 6 7 8 9 10 11 12 19
void main( ) x
g o p i k r i s h n a ‘\0’
{
char x[20];
clrscr( ); Output:
puts(“enter string :”); enter string :
gets(x); gopi krishna
puts(x); gopi krishna
getch( );
}
String functions: -
1. strlen( )
2. strcpy( )
3. strcmp( )
4. strcat( ) <string.h>
Declaration: -
36
Declaration: -
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
else equal
printf(“ not equal ”);
getch( );
}
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);
38
getch( );
}
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
40
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( );
}
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);
41
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( );
}
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
42
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
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,
43
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( )
{
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( );
}
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;
Output:
void main( )
{
struct student s; enter rno :1
clrscr( ); enter name : ravi
printf(“enter rno :”); enter fname : ram
scanf(“%d”,&s.rno); enter hno : 1-2-34/A/2
printf(“enter name :”); enter city : Hyd
scanf(“%s”,s.name); enter per : 70
printf(“enter fname :”);
scanf(“%s”,s.a.fname); rno :1
printf(“enter hno :”); name : ravi
scanf(“%s”,s.a.hno); fname : ram
printf(“enter city :”); hno : 1-2-34/A/2
scanf(“%s”,s.a.city); city : hyd
printf(“enter per :”); per : 70.000000
scanf(“%f”,&s.per);
getch( );
}
UNIONS
Def: -
Collection of different data types.
Declaration: -
46
members of structures; members of structures;
}; }variable;
union tag variable;
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
int x=10,*y,**z; Output:
clrscr( );
y = &x; z = &y; x :10 &x :1000
printf(“x :%d &x :%u \n”,x,&x); y :1000 &y :2000
printf(“y :%u &y :%u \n”,y,&y); z :2000 &z :4000
printf(“z :%u &z :%u \n”,z,&z); *y :10 *z :1000
printf(“*y :%d *z :%u \n”,*y,*z); **z:10
printf(“**z:%d”,**z);
getch( );
}
49
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;
}
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:
50
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( );
}
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);
}
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
fclose(p1); text in first file :
abcdefgh
p2 = fopen(“file2”,”r”); /* text in file2 */
printf(“\n text in second file : \n”); text in second file :
while ( ( c=getc( p2 ) )!=EOF) abcdefgh
putchar( c );
fclose(p2);
getch( );
}
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:
54
for(i=1; i<=10; i++) 1
{ 2
x=getw( p ); 3
printf(“%d \n”, x); 4
} :
fclose(p); 10
getch( );
}
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
} :
55
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
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( )
{
56
printf(“x :%d\n”,x);
x=x+5;
printf(“x :%d\n”,x);
}
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
BINARY SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={33,44,55,22,11},i,j,m,l=0,h=4,key;
clrscr();
printf("enet a key");
scanf("%d",&key);
for(i=0;i<5;i++)
{
m=(l+h)/2;
if(key>a[m])
h=m-1;
else if(key<a[m])
l=m+1;
}
printf("position is %d",m);
getch();
}
LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={33,44,55,66,77},i,key;
clrscr();
printf("enter element to be searched");
scanf("%d",&key);
for(i=0;i<5;i++)
{
if(key==a[i])
printf("element is found at %d position",i);
58
else if(key!=a[i])
printf("\nno such element");
}
getch();
}
SELECTION SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter size of arrary");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
{
printf("enter element at a[%d]",i);
scanf("%d",&a[i]);
}
printf("\nbefore sorting");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nafter sort");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
59
BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={33,44,22,55,11},i,j,temp;
clrscr();
printf("\nbefore sorting");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
printf("\nafter sort");
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(a[i]>a[j+1])
{
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
getch();
}
INSERTION SORT
60
#include<stdio.h>
#include<conio.h>
void main()
{
int temp,a[5]={44,55,22,11,66},i,j;
clrscr();
printf("\n before sort");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
printf("\n after sort");
for(i=0;i<5;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch();
}
MERGE SORT
#include<stdio.h>
#include<conio.h>
61
void main()
{
int a[5]={1,3,5,7,9},b[5]={2,4,6,8,10},c[10],i=0,j=0,k=0;
clrscr();
printf("\nmerge sort\n");
while(i<5&&j<5)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
if(i<5)
{
c[k]=a[i];
k++;
i++;
}
if(j<5)
{
c[k]=b[j];
k++;
j++;
}
printf("merged lements");
for(k=0;k<10;k++)
{
printf("%d ",c[k]);
}
getch();
}
QUICK SORT
#include<stdio.h>
#include<conio.h>
#define maxsize 6
int A[maxsize];
62
{
int rtidx=0,ltidx=0,k=a,l=0,pivot;
int leftarr[maxsize],rtarr[maxsize];
pivot=A[a];
if(a==b)return;
while(k<b) { ++k;
if(A[k]<A[a])
{
leftarr[ltidx]=A[k];
ltidx++;
}
else
{
rtarr[rtidx]=A[k];
rtidx++;
}
}
k=a;
for(l=0;l<ltidx;++l)A[k++]=leftarr[l];
A[k++]=pivot;
for(l=0;l<rtidx;++l)A[k++]=rtarr[l];
if(ltidx>0)quicksort(a,a+ltidx-1);
if(rtidx>0)quicksort(b-rtidx+1,b);
}
void printarr(int a)
{
int i;
for(i=0;i<a;i++)
{
printf("%d",A[i]);
printf("\n");
}
}
main()
{
int i,s;
clrscr();
printf("enter the number of numbers to be entered \n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("enter the number \n" );
scanf("%d",&A[i]);
}
printf("array before sorting ");
printarr(s);
quicksort(0,s-1);
printf("array after sorting");
63
printarr(s);
}
64