1.
CHARACTER SET OF C
1.1 Constant of c language
1.2 Data type in c language
2. VARIABLE
3. OPERATORS
3.1 Arithmetic operator
3.2 Assignment operator
3.3 Logical Operator
3.4 Increment and Decrement operator
3.5 Relational operator
3.6Conditional operator
3.7 Programs
4. DECISION MAKING STATEMENT
4.1 Program with the use of simple if statement
4.2 How to use the if else statement
4.3 How to use the nested if statement
4.4 How to use the else if ladder
4.5 How to use the switch statement
4.6 Solved programs for decision making statement
5. CONTROL STATEMENT OR LOOPING STATEMENT
5.1 Entry control loop
5.2 Exit control loop
5.3 Program demonstration
5.4 Solved program on control statement
6. ARRAYS AND POINTER
6.1 Arrays
6.2 Solved programs on single and two dimensional arrays
6.3 Pointer
6.4 Pointer of pointer
7. FUNCTION OF C LANGUAGE
7.1 Types of function
7.2 CALL BY VALUE AND CALL BY REFERENCE
7.3 Solved program of function
8. STRUCTURE
8.1 Program demonstrate the how to use the structure in program
8.2 Array of structure
8.3 Program demonstrate use of dynamic memory allocation with structure
8.4 Program demonstrate function in structure
8.5 Demonstrating the structure array variable
8.6 Passing and returning the structure variable from the function
8.7 Demonstrating nesting of structure.
8.8 Demo of pointer variable of structure
9. UNION
9.1 Program demonstrate the use of union
10. STRING HANDLING IN C
10.1 Demonstrating the string array
10.2 Use of gets() and puts() function
10.3 STRING HANDLING FUNCTION
11. FILE HANDLING
11.1 What is a File?
11.2 Steps in Processing a File
11.3 The basic file operations
11.4 File Open Modes
1. CHARACTER SET OF C
Character set of c language used for creating application of c or
creating the program of c language means using the combination of
character set us can create the c programming statement.
C provide the following character set to us
Alphabet: a to z or A to Z
Digit: 0 to 9
Special Symbol: ?,+,<,>etc.
How to recognize the character set from system
The system cannot understand the high level language. So c provide the
some basic Code translation language is called as ASCII(American Standard
Code for Information and Interchange) with the help of this character set c
recognize the characters in programmer and store in some specific format in
system and then convert it in the machine code with the help of compiler.
Given table shows the ASCII code chart of alphabet and digits.
A to Z: 65 to 90 and a to z: 97 to 122
Digit: 0 to 9:48 to 57
Total ASCII code in c language is 256 from 0 to 255
1.1 Constant of c language
Constant means those values cannot change during the execution of
program is called as a constant.
Types of constant
Numeric Constant
In this type of constant we can fix only number value in program.
Types of Numeric Constant:
1] Integer Constant:-
This type of constant cannot have decimal point
e.g.12, 13,09 etc.
2] Float Constant or real constant:-
This type of constant can have decimal point
e.g.1.2, 3.2, 5.4 etc.
3] Character Constant:-
In character Constant we can store the single character constant and more
than one character constant.
4] Single character constant:-
If we fix the any value in single coat is called as character constant.
e.g. a, A, 3 three is not a numeric constant this is the character
constant because in singlecoat.
5] Multicharacter Constant:-
If we fix the any value in double coat is called as multi character
constant or in c string.
e.g.a[]={good};
Keywords of c language:-
Keywords mean those worlds having the single meaning provided by
compiler.We can say keywords are reserve word. There are 32 keyword in c
given table show all keyword of c language.
E.g. if we want to write the integer in ccannot write the integer full. We
need to write the int because compiler knows int means integer because int is
the keyword.
1.2 Data type in c language
Types of data type in c language:-
1] Built in data type:-
Those data type are provided by compiler is called as built-in data type.
Types of built in datatype
A] Integer data type:-this data type indicates to us only use the value without
decimal point.
e.g.90,100,03;//allow with integer.
e.g.1.2,3.6;//not allow with integer.
Integer also provides the some its sub datatype:
i] Integer
If we want to use the integer data type in programme write simply:
int
Integer datatype allocate the 2 byte memory
Integer datatype can store the value-32768 to 32767
Integer datatype use the format specifier %d
ii]Short Integer
If we want to use the short integer in programme write simply,
short int.
Shot int data type allocate 1 byte memory
Short integer can store the value -32768 to 32767
Short integer datatype use the format specifier %d or %i.
iii] Long Integer:
If we want to use the long integer in programme write simply :long
int
Long Integer data type allocate 4 byte memory
Long Integer datatype can store the value -2147483648 to
2147483647
Long Integer use the format specifier %ld
iv] Unsigned Integer:
If we want to use the unsigned integer in program write simply:
unsigned int
Unsigned Integer data type allocate 2 byte
Unsigned integer data type can store the value 0 to 65535
Unsigned format specifier: %u
B] Float data type:-float data type store the value with decimal point
e.g. a=1.4,b=4.5; etc. allow with float.
e.g.if we store the integer value then it will accept and place (0)at last
value.
Float also provides the some its sub datatype.
i]Float:-
If we want to use the float datatype use in program write simple:-
float
Float data type allocate 4 byte memory
Float datatype can store value -3.14x10e 38 to 3.14x10e 38
Float data type use the format specifier %f
ii]Double:-
If we want use the double datatype in program write simply:-
double.
Double datatype allocate memory 8 byte.
Double datatype can store the value -1.7x10 e 308 to 1.7x 10e308
Double datatype use the format specifier %lf
iii]Long Double:-
If we want to use the long double data type in program write
simply: long double
Long double datatype allocate 8 byte memory
Long double datatype can store the value -1.7 x 10e4932 to 1.7 x
10e4932
Long double datatype use the format specifier
C] Character Data type:-
This data type store only alphabetical data.
Types of character datatype
1] Signed Character
2] Unsigned Character
a]Derived data type:- Those datatype we can use with any other datatype.
Types of derived datatype:
i] Pointer
ii]Function
iii]Array
b]User Defined data type:- Those data type created by user for its own use is
called as user defined datatype.
Types of userdefined datatype:
i]Structure
ii]Union
iii]Enumeration
2. VARIABLE
Variable means if we create the variable in program then system allocate
the block for that variable in memory as per its type.
Why use the variable:
Variable used to store the value in memory as per its type
Variable also used to retrieve the value from memory for display
the output for some process.
How to create the variable in c
Syntax:- data type variable name separated by commas;
E.g.int a,b,c;
Above statement shows we create the three variable of type integer
namely a,b,c and We can store the three integer type values in that but if we
want to create the variable in c language for that we need to follow some rule
of c language for variable declaration.
Rule of c language for variable declaration
Variable means name start with alphabet cannot start with digit or
any special Symbol other than underscore character.
e.g.
int a10;//valid variable name
int _a10;//valid variable name
int 10a;//not valid variable name
int ?a;//invalid variable name
Cannot use the blank space between two variable.
e.g.
int a b;//invalid variable name
int a_b;//valid variable name
Cannot give the keyword variable name
e.g.
int float;//invalid variable name
How to write the application
Steps for application write in c language.
Add the header files.
How to add the header files in program
If we want to add the header file in c program for that we need to use the
#include preprocessor directives or file inclusion directives. This directive is
used to add the any file in program. If we add the file once in program we can
use the all contents form that file In your program.
Syntax:- #include<filename.extension>
e.g.#include<stdio.h>
Write main function
How to write the main function
Returntype main()
Write here the body of programme
e.g. void main(){
printf(good morning);
getch();
Write program code
Define function after the main if use function in program
Basic application of c language
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
printf(good morning.);
getch();
Output:
What is use of stdio.h and conio.h header file
Stdio.h= standard input output function. Header file this file
provides the all input and output related functions to us.
Conio.h= console input output function.this file is used to device
control file means if we want to clear the output screen or hold the
output on dos prompt for that we need to use the conio.h file.
Void main():- this is the heart of c programmer means we cannot
run the c program. Without main function .for every c program must
be single main function. Every c program execution start with main
function
Clrscr():- Clear the screen
This function is used to clear the output screen. This function
is provided by conio.h header file.
Printf():- This is the output function this function is used to
display the output on the screen.
Getch():-This function is used to hold the output screen on dos
prompt whenever user press any key when user press any key then
come back in programme,
Input and output function of c
If we want to interact with c program then we need to provide the
input to program and get the output from program for this purpose c provide
the some input and output related function to us. If we want to use the input
and output functions of c language for that we need to use the stdio.h header
file.
Input function:-
Putchar():- this function is used display the single character output
Getchar():-this function is used to read the single character from
keyboard
Gets():- this function is used to accept the string output from keyboard.
Puts():-this function is used to display the string output on monitor
Printf():-this function is used to display the any type of data on output
screen
Scanf():-this function is used to read the any type of data from keyboard.
How to use the putchar() and getchar() function shows in following
programme
Syntax of getchar() function:-
Charactervariable=getchar();
e.g. ch=getchar()this statement indicate to us read the single character
data from keyboard
Syntax of putchar():-
Putchar(character variable);
e.g. putchar(ch);
programme demonstrate the use of getchar() and
putchar() function
#include<stdio.h>
#include<conio.h>
void main()
char ch;
clrscr();
printf(enter the character data\n);
ch=getchar();
putchar(ch);
getch();
Output:
How to use the printf()function
Printf() can use for three purpose
Display the message
Syntax:-printf(message write here for display);
e.g. printf(good morning);
Display the variable values
Syntax:- printf(format specifier,variable name);
e.g. printf(%d,c);
Display the message with variable value.
Syntax:- printf(message format specifier ,variable);
e.g. printf(addition is %d,c);
How to use scanf() function
Syntax:- scanf(format specifier,&variablename);
e.g. scanf(%d,&a);
programme demonstrate the use scanf() and printf() function
Write a program to accept the two values and display
its multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(enter the two values);
scanf(%d%d,&a,&b);
c=a+b;
printf(addition is %d,c);
getch();
Output:
3. OPERATORS
Operators are the symbol they use for perform the some operation.
There are seven types of operator in c language.
3.1 Arithmetic operator
Operator meaning
+ Addition
- Subtraction
* Multiplication
/ Division
3.2 Assignment operator
Operator Meaning
Assignment or
put right value
=
in to left
variable
3.3 Logical Operator
Operator Meaning
&& Logical and
|| Logical or
! And
3.4 Increment and Decrement operator
Operator meaning
increment by
++
default one
Decrement
--
bydefault one
3.5 Relational operator
Operator meaning
== equal to
!= not equal to
> Greater than
< less than
<= less than equal to
>= Greater than equal to
3.6Conditional operator
Operator meaning
? Ternary operator
: colon operator
Format of conditional operator
Syntax : exp1:exp2:exp3;
e.g. a>b?printf(good morning);:printf(bad);
expression one always condition and expression second and third are
conditions. If expression one true then execute the expression two if expression
one is false then execute the expression third.
3.7 Programs
1] Find the area of circle and circumference of
circle
#include<stdio.h>
#include<conio.h>
void main()
int radius ;
float area,circum;
clrscr();
float pi=3.14;
printf(\n enter the radius of the circle:);
scanf(%d,&radius);
area=pi*radius*radius;
circum=2*pi*radius;
printf(\n the area of the circle is %2f,area);
printf(\n the circumference of the circle is %2f,circum);
getch();
Output:-
2] Program demonstrate to calculate the bill
discount
#include<stdio.h>
#include<conio.h>
void main()
int items;
float discount ,bill,price;
clrscr();
printf(enter the number of items purchased:);
scanf(%d,&items);
printf(enter the price of items purchases);
scanf(%f,&price);
bill=items*price;
printf(the bill before discount %2f,bill);
discount=0.2*bill;
printf(\n the discount is %2f,discount);
bill=bill-discount ;
printf(\n the final bill is %2f,bill);
getch();
Output:
3] Program demonstrate the c arithmetic
feature
#include<stdio.h>
#include<conio.h>
void main()
int x=10,y=10,float z;
clrscr();
z=x+y;
printf(\n value of z is %d,z);
x=x+y;
printf(\n value of x is %d,x);
y+=z;
printf(\n value of y is %d,y);
x++;
printf(\n value of x is %d,x);
y=x++;
z=++y;
printf(\n value y is %d,y);
printf(\n value of z is %d,z);
getch();
}
4] Program demonstrate finding the sum of
digit
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int num,rem,sum=0;
printf(enter any number of 4 digit);
scanf(%d,&num);
rem=num%10;
sum=sum+rem;
num=num/10;
rem=num%10;
sum+=rem;
num/=10;
rem=num%10;
sum+=rem;
num/=10;
sum+=num;
printf(\n the sum of the digits is %d,sum);
getch();
}
4. DECISION MAKING STATEMENT
Decision making statement means if we have number of choices in
program and we want to select the particular choice from then we need to
check the condition and select the option when we want.
Ways of decision making statement in c language.
1] Simple if statement
2] If else statement
3] Else if ladder statement
4] Nested if statement
5] Switch statement
How to use simple if statement
Syntax:- if(condition)
The statement block;
Other program part.
Explanation:-
In simple if statement if condition is true then execute the if block if
condition
False then execute the other program part.
e.g. consider in program a=10 and b=5
if(a>b)
Printf(A is a greater %d,a);
If(b>a)
Printf(B is a greater %d,b);
In above example the program generate the output : a is greater
10because
A is a greater then b and condition is true.
4.1 Program with the use of simple if statement
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
printf(enter the two values);
scanf(%d %d,&a,&b);
if(a>b)
printf(A is greater %d,a);
if(b>a)
printf(Bis greater %d,b);
getch();
}
Output:-
4.2 How to use the if else statement
If (condition)
True statement block
Else
False statement block
Other programming statement
Explanation:- in if else statement if condition is true then
execute the if block otherwise execute the else block.
#include<stdio.h>
#include<conio.h>
void main()
int no;
printf(enter the number from keyboard);
scanf(%d,&no);
if(no%2==0)
printf(Number is even %d,no);
else
printf(Number is odd %d,no);
getch();
Output:-
4.3 How to use the nested if statement
In nested if statement if outer if statement is true then execute
the inner if statement if inner if statement is true then execute the
inner if true statement block otherwise execute the else block of
appropriate if statement.
Syntax:-
if (condition)
if(condition)
true statement block
Else
false statement block
else
{
statement block
other program part
programme demonstrate the use of nested if
statement
Write a program to enter the id and salary of employee if id is equal to 1
or 2 and salary is greater than 5000 then employee is permanent. If salary is
less than5000 then employee is temporary. If id is not equal to q or 2 then
employee is not a member of organization.
#include<stdio.h>
#include<conio.h>
void main()
int id,sal;
clrscr();
printf(enter the id of employee \n);
scanf(%d,&id);
if(id==1||id==2)
{
printf(enter the salary of employee \n);
scanf(%d,&sal);
if(sal>5000)
printf(employee is permanent);
else
{
printf(employee is temporary \n);
else
printf(employee is not a member of organization);
getch();
Output:
4.4 How to use the else if ladder
In else if ladder every if statement execute self no condition
is depend on another condition if first if statement is true then
execute otherwise check the another else if condition if anyone
else if statement is not true then execute the default
Else block
Syntax:- if(condition)
true statement block
else if (condition )
True statement block
else
false statement block
}
Other program part
Program demonstrate the use of else if ladder
statement
Program to enter the three digit number and
check which is greater.
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
printf(enter the three number );
scanf(%d%d%d,&a,&b,&c);
if(a>b&&a>c)
printf(a is the greater %d,a);
else if(b>a&&b>c)
{
printf(b is the greater %d,b);
Else
printf(c is the greater %d,c);
getch();
Output:
4.5 How to use the switch statement
Syntax:-
Switch(choice)
case 1:
statement
break;
case 2:
statement
break;
case 3:
statement
break;
default:
Statement
}
In switch if case match with given choice then the particular
case block will execute.
Note:-in switch statement we can pass the character as a
choice. any arithmetic expression without decimal point but cannot
pass the any floating value as a choice as well as string.
Following programs demonstrate the use of
switch statement
#include<stdio.h>
#include<conio.h>
void main()
int choice;
clrscr();
printf(enter the your choice);
scanf(%d,&choice);
switch(choice)
{
case 1:
printf(your choice is %d,choice);
break;
case 2:
printf(your choice is %d,choice);
break;
case 3:
printf(your choice is %d,choice);
break;
default:
printf(your choice is wrong);
getch();
Output:-
4.6 Solved programs for decision making statement
Program for calculate the electricity bill
#include<stdio.h>
#include<conio.h>
void main()
int units;
float bill;clrscr();
printf(\n enter the number of units consumed);
scanf(%d,&units);
if(units<=100)
bill=units*2.35;
else
if(units<=500)
bill=100*2.35+(units-100)*5.65;
else
if(units <=1000)
bill=235+400*5.65+(units-500)*7.35;
else
bill=235+400*5.65+500*7.25+(units-1000)*9.85;
printf(\n the bill is %2f,bill);
getch();
Output:
Program demonstrate the discount on bill
#include<stdio.h>
#include<conio.h>
void main()
int items;
float discount ,bill,price;
clrscr();
printf(\n enter the number of items purchased:);
scanf(%d,&items);
printf(\n enter the price of items purchased:);
scanf(%f,&price);
bill=items*price;
printf(\n the bill before discount %2f,bill);
if(bill>1000)
discount =bill*0.1;
bill=bill-discount;}
else
if(bill>5000)
discount =bill*0.1;
bill=bill-discount;
printf(\n the final bill is %2f,bill);
getch();
}
Output:-
Program demonstrates the number is positive
or not
#include<stdio.h>
#include<conio.h>
void main()
int num;
clrscr();
printf(\n enter the number );
scanf(%d,&num);
if(num>0)
printf(\n the number is positive);
if(num<0)
printf(\n the number is negative);
if(num==0)
printf(\n the number is zero);
getch();
Output:-
Program for calculating the salary on hour
basis
#include<stdio.h>
#include<conio.h>
void main()
int items;
float discount,bill,price;
clrscr();
printf(\n enter the number of items purchased);
scanf(%d,&items);
printf(\n enter the price of items purchased);
scanf(%d,&price);
bill=items*price;
printf(\n the bill before discount %2f,bill);
if(bill>1000){
discount =bill*0.1;
bill=bill-discount;
else
if(bill>5000)
discount =bill*0.1;
bill=bill-discount;
else
if(age<22&&(gender==m||gender==m)&&
(cat==s||cat==s)&&per>60)
printf(\n Eligible);
else
printf(\n not eligible);
getch();
Output:-
5. CONTROL STATEMENT OR
LOOPING STATEMENT
Control statements are use if we want to perform the some task
repetitively again and again.
Following are control statement in c language.
5.1 Entry control loop
Entry control loop means if condition satisfy or true first time then loop
will be execute otherwise not
Types of entry control loop
i] for loop
ii] while loop
5.2 Exit control loop
May be condition false first time loop will execute minimum single time
is calledas exit control loop .if condition is true then loop will execute same as
entry control loop.
If we want to use the any type of loop we need three important steps.
i] Initialization: initialization means starting point of loop
ii] Condition:-how many time will be repeat or how may be repetition
you want to perform.
iii] Increment and Decrement:- steps for increment and decrement
5.3 Program demonstration
How to use the for loop in program or application
Syntax:- for(Initialization;condition;increment and
decrement)
Body for loop
e.g. for(i=1; i<=5; i++)
/* Initialization Condition Increment &Decrement
*/
Printf(\n good morning);
Output:-
Good morning
Good morning
Good morning
Good morning
Good morning
How to use execute above example:-
First time i=1 is less than 5 then condition is true and loop will be
execute and print the good morning message then I will increment by 1 then
next time i=2 this is also true so loop will be execute and print the good
morning message this process done up to the five times.
Following program demonstrate the use of for loop for
print the table
#include<stdio.h>
#include<conio.h>
void main()
int no,table,I;
clrscr();
printf(enter the number for table \n);
scanf(%d,&no);
for(i=1;i<=100;i++)
{
table=no*I;
printf(%d\n,table);
getch();
Output:-
How to use the while loop in program
Syntax:- Initialization ;
While(condition)
{
Body of while loop
Increment and decrement
e.g. i=1;//initialization or 1 start from 1
while(i<=5)
Printf(good morning);
I++;
How to use the do while loop
Syntax: - Initialization;
Do
Statement block
Increment or decrement
While(condition);
Following programs demonstrate the use of do
while loop for display the table
#include<stdio.h>
#include<conio.h>
void main()
int no,table.i;
clrscr();
printf(enter the number for print table \n);
scanf(%d,&no);
i=1;
while(i<=no)
{
table =i*no;
printf(%d\n,table);
i++;
getch();
}
Use of break and continue keyword
Break: - This keyword is used to stop the execution of loop in
middle if programmer wants.
Continue: - This keyword is used to skip some the execution step
of loop if programmer wants.
Program demonstrates the use of break
keyword.
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<=5;i++)
if(i==3)
break;
else
printf(good morning);
getch();
Output:-
This program generate the output good morning two times because
condition satisfy in loop then loop will stop this execution
Nested for loop statement
For(Initialization ;condition;increment and decrement)
{
For(initialization; condition;increment and decrement)
Body of inner for loop
Body of outer for loop
Program demonstrate the use of nested for
loop
#include<stdio.h>
#include<conio.h>
void main()
int I,j,table;
clrscr();
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
tacle=i*j;
printf(%d,table);
printf(\n);
getch();
Output:-
Write program calculate the marks.
#include<stdio.h>
#include<conio.h>
void main()
int marks1,marks2,marks3,sum;
char choice;
clrscr();
do
printf(\n enter the marks of three subjects);
scanf(%d%d%d,&marks1,&marks2,&marks3);
sum=marks1+marks2+marks3;
printf(\n sum is %d,sum);
printf(\n cont(Y/N):);
fflush(stdin);
choice=getche();
while(choice!=n&&choice!=N)
getche();
Output:
Program for fibonancci series
#include<stdio.h>
#include<conio.h>
void main()
long first=0,second=1,third;
int i,num;
clrscr();
printf(how many numbers u want in the series:);
scanf(%d,&num);
printf(\n %ld\t%ld,first,second);
for(i=2;i<num;i++)
third=first+second;
printf(\t %ld,third);
first=second;
second=third;
getche();
Output:-
Program for check prime or not
#include<stdio.h>
#include<conio.h>
void main()
int num,i,rem;
clrscr();
printf(\n enter the number);
scanf(%d,&num);
for(i=2;i<num/2;i++)
rem=num%i;
if(rem==0)
printf(\n the number is not prime);
break;
}
}
if(i==(num/2))
printf(\n the number is prime);
getch();
Output:-
Print the star shape using nested loop
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
clrscr();
printf(\n first\n);
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf(*\t);
printf(\n);
printf(\n second \n);
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(*\t);
printf(\n);
printf(\n Third \n);
for(i=1;i<=5;i++)
{
for(k=4;k>=i;k--)
printf(\t );
for(j=1;j<=i;j++)
printf(*\t);
printf(\n);
getch();
Output:-
Reversing the number and also counting its
number of digits
#include<stdio.h>
#include<conio.h>
void main()
long num,rev=0,temp;
int rem,count=0,sum=0;
clrscr();
printf(\n enter the number);
scanf(%ld,&num);
temp=num;
while(num>0)
rem=num%10;
num=num/10;
count++;
sum+=rem;
rev=rev*10+rem;
printf(\n the number of digits in %ld is %d ,temp,count);
printf(\n the sum of the digits of %ld is %d,temp,sum);
printf(\n the reverse of %ld is %ld,temp,rev);
getch();
Output:
Program to show the good morning message
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<=5;i++)
if(i==3)
continue;
else
printf(good morning);
}
getch();
Output:-
This program four time good morning message. If condition
satisfies in loop then it will skip one step of execution in loop.
5.4 Solved program on control statement
Print the series of Armstrong number up to
the limit.
#include<stdio.h>
#include<conio.h>
void main()
int num,sum,temp,rem,limit;
clrscr();
printf(\n enter the limit:);
scanf(%d,&limit);
for(num=1;num<=limit;num++)
sum=0;
temp=num;
while(temp>0)
{
rem=temp%10;
sum=sum+rem*rem*rem;
temp/10;
if(sum==num)pPrintf(\t %d,sum);
getch();
Output:-
Check number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,temp,rem;
clrscr();
printf(\n enter the number);
scanf(%d,&num);
temp=num;
while(num>0)
rem=num%10;
sum=sum+rem*rem*rem;
num/=10;
if(sum==temp)
printf(\n the number is a armstrong);
else
printf(\n the number is not Armstrong);
getch();
Output:-
6. ARRAYS AND POINTER
6.1 Arrays
Array is a derived datatype which is used to store the more than one
values in single variable but same type.Means we can say array is a collection
of values of similar datatype.
Types of arrays
I]one dimension array
ii] Multi-dimensional array.
The identification of array is:[] {subscripting or dimension}
If we place the subscript with any type variable it is become array
How to declare the single dimension array
Syntax:- datatype variable name[size];
e.g. int a[5];
Above statement shows variable a is a integer type array and can store
and can store the five values in it because mention in array declaration.
After the array declaration how array stored in memory
Array stored in memory sequentially means one by one.the first element
of array stored at location zero index. The second element at one index and so
on n-1.
The following diagram shows how array stored in memory as per above
example.
0 1 2
3 4-Index
First Second Third Fourth Fifth
values Values Values Values Values
100 102 104
106 110 ->Location
Indexes:- Indexes are used to insert the element in array delete the
specified element from array search element of array is operation perform
on array.
Location:-Location indicate As per datatype size.
How to store the values in single dimension array.
For(variable=0;variable<sizeof array; variable++)
Scanf(%d,&array variable(variable));
e.g.
for(i=0;i<5;i++)
Scanf(%d,&a[i]);
How to shows the values of array retrieve the values of array
For(variable=0;variable<size of array;variable++)
Printf(%d,arrayvariable(variable));
e.g. for(i=0;i<5;i++)
Printf(%d,a[i]);
}
Program using the single dimension array
store the five values in array and display it
#include<stdio.h>
#include<conio.h>
void main()
int a[5],i;
clrscr();
printf(store the values in array);
for(i=0;i<5;i++)
scanf(%d,&a[i]);
printf(display the values of array);
for(i=0;i<5;i++)
printf(a[%d]------%d\n,I,a[i]);
}
getch();
Output:-
How to use the two dimensional arrays
Syntax:- datatype variablename[size][size];
e.g.int a[3][4];
generally two dimensional array is used to create the
matrix then first bracket refer to the row and second bracket
refer to thecolomn.if we want to store the values in two
dimensional array then we need to nested two loop.
How to store the values in two dimensional array
For(i=0;i<first bracket size;i++)
For(j=0;j<second bracketsize;j++)
Scanf(%d,&array variable[i][j]);
e.g. for(i=0;i<3;i++)
for(j=0;j<4;j++)
Scanf(%d,&a[i][j]);
How to shows or retrieve the two
dimensional array
for(i=0;i<first bracket size;i++)
{
for(j=0;j<3;j++)
scanf(%d,&a[i][j]);
printf(\n Display the matrix);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(%d\t,&a[i][j]);
printf(\n);
getch();
}
6.2 Solved programs on single and two
dimensional arrays
1] Program shows adding two array and
storing in third array
#include<stdio.h>
#include<conio.h>
void main()
int num1[10],num2[10],sum[10],i,j,k;
clrscr(();
printf(\n enter ten element of the first array:\n);
for(i=0;i<10;i++)
scanf(%d,&num1[i]);
printf(\n enter ten element of the second array);
for(i=0;i<10;i++)
scanf(%d,&num2[i]);
printf(\n the addition of two arrays is \n);
for(i=0;i<10;i++)
sum[i]=num1[i]+num2[i];
printf(\n%d,sum[i]);
getch();
Output:-
2] Program demonstrate the counting
,positive,negative,even,odd,values in arrays
#include<stdio.h>
#inlclude<conio.h>
void main()
int num[10],i,pos=0,neg=0,even=0,odd=0,zero=0;
clrscr();
printf(\n Enter the elements of the array);
for(i=0;i<10;i++)
scanf(%d,&num[i]);
for(i=0;i<10;i++)
if(num[i]>0)
pos++;
else
if(num[i]>0)
pos++;
else
if(num[i]<0)
neg++;
if(num[i]==0)
zero++;
else
if(num[i]%2==0)
even++;
else
odd++;}
printf(\n the positive number are %d,pos);
printf(\n the negative number are %d,neg);
printf(\n the even number are %d,even);
printf(\n the odd number are %d,odd);
printf(\n the zero number are %d,zero);
getch();
Output:-
3] Program for calculate the square of even
position and cube of odd position
#include<stdio.h>
#include<conio.h>
void main()
int num[10],i;
clrscr();
printf(\n enter the ten element of the array\n);
for(i=0;i<10;i++)
scanf(%d,&num[i]);
printf(\n the elements of the array after squre and cube
are\n);
for(i=0;i<10;i++)
if(i%2==0)
num[i]=num[i]*num[i];
printf(%d,num[i]);
else
num[i]=num[i]*num[i]*num[i];
printf(%d,num[i]);
getch();
Output:
4]Program for converting decimal to binary
#include<conio.h>
#include<stdio.h>
void main()
int num,i=0,j;
int binary[20];
clrscr();
printf(\n enter the number);
scanf(%d,&num);
while(num>0)
{
binary[i]=num%2;
num=num/2;
i++;
printf(\n the binary equivalent is);
while(i>0)
i--;
printf(%d,binary[i]);
getch();
Output:-
5]Program find greatest and smallest
element in arrays.
#include<stdio.h>
#include<conio.h>
void main()
int num[10],i,great,small,gpos,spos;
clrscr();
printf(enter the elements of the array);
for(i=0;i<10;i++)
scanf(%d,&num[i]);
great=small=num[0];
gpos=spos=0;
for(i=1;i<10;i++)
if(num[i]>great)
great=num[i];
gpos=i;
}
if(num[i]<small)
small=num[i];
spos=i;
printf(\n the greatest number %d,great);
printf(\n the greatest number is %d,gpos);
printf(\n the smallest number is %d,small);
printf(\n at position %d,spos);
getch();
Output:-
Program for generating transpose matrix
#include<stdio.h>
#include<conio.h>
void main()
int num[5][3],tran[3][5],i,j;
clrscr();
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf(%d,&num[i][j]);
}
printf(\n the original matrix is \n);
for(i=0;i<5;i++)
for(j=0;j<3;j++)
printf(%d,num[i][j]);
getch();
}
6.3 Pointer
Pointer is a derived datatype which is used to store the
address of memory location for creating the dynamic memory
allocation.
What is the meaning of dynamic memory allocation:
Dynamic memory allocation means to decide how much
memory required at run time is called as dynamic memory
allocation.
What is the benefit of dynamic memory allocation:
If suppose we use array then we cannot give the more
element than array size and if we give the less element decided
by array size then remaining memory elements will be lost this
is the drawback of array or compile time memory allocation or
static memory allocation so we need to use the pointer.
What is the identification of pointer:
*----- is the identification of pointer (value at address
operator or indirection operator) If we give the * with any type
variable we can say this is the variable of pointer type.
e.g. int a;//this statement says is a variable of type in but
if we place the *before the variable a.
e.g. int *a;//this statement says a is a pointer variable of
type integer means a can store the integer memory block in it or
integer type memory address in it.
How to store address of any another variable in pointer
variable:
If we want to store the any another variable or memory
address in pointer variable for that use the & operator. & is the
address of memory address. This operator is used to access the
address memory location or variable.
How to store the address of any another variable in pointer
variable:
Syntax:- pointervariable=&normal variable;
e.g. a=&b;
as per the reference of the example a is a pointer variable
and be the normal or general variable means b address pointer
variable a. what happen if b address stored in a. if we apply the
change on variable a indirectly effect on b (note: this will
discuss in function call by value call by reference );
Following example shows how to store the
address of normal variable in pointer
variable and access its value.
#include<stdio.h>
#include<conio.h>
void main()
int *ptr,b=10;// ptr is a pointer and b is a normal variable.
ptr=&b;//store the address of b in pointer variable a
printf(address of b is %u\n,ptr); //ptr displays the
address of b because mention in above .
printf(address of b is %u\n,&b);//print b with address
operator so print the address of b
printf(value of b is through ptr =%d,*ptr);//if we give
the start with any pointer variable then .
//if will give the value of variable whose address store in
it so *is a value at address operator.
getch();
Output:-
Note:- address value change on every pc because address
allocation decide the use of ram on that pc.
6.4 Pointer of pointer
What is the pointer of pointer
If we want to store the address of one pointer variable in to the
another pointer variable for that we need to use the pointer of pointer in c.
How to declare the pointer of pointer
Syntax:- datatype **variablename;
e.g. int **a;// this statement say can store the two address field in it
means a can point to two memory location at a time.
How to store the address in pointer of pointer variable
Int **ptr.*a,b;
B=10;//b contain the value 10.
A=&b;//a contain the address of b
Ptr=&a;//ptr contain the address pointer variable a as well as
variable b.because ptr has ability
To store the two address field because ptr having two star(indirection
pointer).
Program demonstrate the use of pointer of
pointer.
#include<stdio.h>
#include<conio.h>
void main(){
int **ptr,*a,b=10;
a=&b;
ptr=&a;
printf(address of a is %u\n,ptr);//print the address of a
printf(address of b is through ptr =%u,*ptr);//print the value of a
means address of b
printf(value of b is through ptr=%d,**ptr);//access the value at
address stored in a
getch();
Output:
How to create the dynamic memory allocation using the pointer
If we want to create the dynamic allocation using pointer in c for that
c provide the one file to us called as allocation h file.in this file contain the
some functions those are responsible for creating the dynamic memory
allocation.
Some of the function given below
1]Malloc:- malloc is a dynamic memory allocation function.malloc
allocate block of size byte from memory heap it allow a programmer to
allocate memory explicitly as it is needed and in the exact amount of
needed. The heap is used for dynamic allocation of variable size block of
memory .many data structure, such as trees and lists, naturally employ heap
memory allocation.
How to use the malloc function in program
Syntax:-pointervariable=(datatype*)malloc(sizeof(size));
e.g. ptr=(int *)malloc(sizeof(10);
2]calloc
3]realloc
4]Free
Example on dynamic memory allocation
using the pointer
Demo of malloc function
#include<stdio.h>
#include<conio.h>
void main()
int *ptr,i;
clrscr();
ptr=(int *)malloc(sizeof(int));
printf(\n address in ptr is %u,ptr);
printf(\n Enter the value :);
scanf(%d,&ptr);
printf(\n\n value at address in ptr is %d,*ptr);
getch();
getch();
}
Output:
Program demonstrate the dynamic array.
#include<stdio.h>
#include<conio.h>
void main()
int *ptr,i,size;
clrscr();
printf(\n enter the size);
scanf(%d,&size);
ptr=(int *)malloc (size*sizeof(int));
printf(\n address in ptr is %u,ptr);
for(i=0;i<size;i++)
{
scanf(%d,ptr);
ptr++;
printf(\n address in ptr is %u,ptr);
ptr=ptr-size;
getch();
Printf(\n address in ptr is %u,ptr);
Printf(\n\n);
For(i=0;i<size;i++)
Printf(\n %d,*ptr);
Ptr++;
getch();
Return 0;
Output:
7. FUNCTION OF C LANGUAGE
Function is a self-statement of block which is used to divide the large
program in to sub module or small part for simplicity of application or
program.
What is a benefit of function.
The main benefit of function is define once and use multiple times
means if we want to reuse the function code simply call it.
7.1 Types of function
There are two types of function.
1] library function:-library function means those function provided
by c language use cannot edit in library function.
e.g. printf(),scanf()etc.
Cannot write print() or scanf() etc.
2] User defined function:- those function defined by use for its
own use is called as user define function. User defined function name
depend on user choice. Remember if we want to use the any user
defined function for that we need to use the three important things.
i] Function declaration /prototype of function
Declaration specify which function we will use in program its name
as well as its return fun type as well as how many values function can
transfer and its datatype.
ii] Function calling:- function call means execute or run the body of
function or definition function calling generally use for reuse the function
definition.
iii] Function definition/called function:- function definition means
write the actual body of function or write the code of function.
Types of user define function
1] Function with returning value with argument or parameter
2] Function without returning value with argument
3] Function without returning value without argument.
4] Function with returning value without argument.
How to declare the function in program
Syntax:- returntype function name (datatype);
e.g. int add(int,int);
above declaration says we will use the user define function in
program namely add and this function return the integer type value and this
function can pass the two values of type integer .
note:- if we give the return type of function as void then function
cannot return the any value.
How to call the function in c language
1] if function return values how to call function in function has return
type.
Return value=function name(variable name as per declaration);
e.g. c=add(a,b);
here in above example a,b,c must be integer type because declaration
says.
2] if function not return the value
How to call the function if function has no return type
Function name(variable);
e.g.add(a,b);
Here add cannot return the value if add() function return type is void.
How to define the function in c program
e.g. return type function name (data type variable name,datatype
variable name)
Body of function
e.g. int add(int x,int y)
Return(x+y);
}
Note:- cannot define function in main function this is the rule of c
language
Function define must be before main of after after main.
1] Program using function with return type
with argument
#include<stdio.h>
#include<conio.h>
void main()
int no,sq;
int squre (int);
clrscr();
printf(enter the number for squre \n);
scanf(%d,&no);
sq=squre(no);
printf(squre of number is %d,sq);
getch();
}
Int squre(int x)
Return(x*x);
Output:-
2. Program using without return value
without argument.
#include<stdio.h>
#include<conio.h>
void main(){
void squre();
clrscr();
squre();
getch();
void squre ()
{
int no;
printf(enter the value for squre\n);
scanf(%d,&no);
printf(squre of number is %d,no*no);
Output:-
3.Program with returning value without
argument
#include<stdio.h>
#include<conio.h>
void main()
{
int squre();
clrscr();
printf(squre of number is %d,squre());
getch();
void squre ()
int no;
printf(enter the value for squre\n);
scanf(%d,&no);
return(no*no);
Output:-
7.2 CALL BY VALUE AND CALL BY
REFERENCE
Call by value:-
Call by value means actual argument pass the photo copy to formal
argument or calling function pass the duplication value to call or the
definition of function. Means if we made any change on formal argument not
affect on actual argument.
Actual argument means calling function argument and formal argument
means called function argument.
Following program demonstrate
the call by value.
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
void swap(int ,int);
printf(enter two values);
scanf(%d %d,&a,&b);
swap(a,b);
printf(A=%d\t B=%d\n,a,b);
getch();
void swap(int x,int y){
temp;
x=y;
y=temp;
printf(X=%d\t Y=%d\n,x,y);
Output:-
This program output
X=20 y=10
X=10 y=20
If we swap the values in definition not effect on calling function
argument.
So the drawback of call by value is cannot access the more than
one return value from function because function return the single value
from function at a time.So the we need to call by value and call by
reference we send the address of calling function argument to called
function argument or actual argument address to the formal address
Call by reference:- in call by reference we need to pass the actual
argument address to formal argument so we need to declare argument type
is pointer declaration.
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
clrscr();
void swap(int *,int *);
printf(enter the two values );
scanf(%d%d,&a,&b);
swap(&a,&b);
printf(A=%d\t B=%d\n,a,b);
getch();
void swap(int *x,int *y){
temp=*x;
*x=*y;
*y=temp;
printf(X=%d\t Y=%d\n,x,y);
This program generate the output
X=10 y=20
A=20 B=10.
FUNCTION RECURSION
Function recursion means when function call itself again
and again is called as function recursion. Function recursion is
a same like as loop.
OR
Call same function within its definition is called as
function recursion.
Following program shows the use of function
recursion.
#include<stdio.h>
#include<conio.h>
void main()
void recursion(int);
int value;
clrscr();
printf(enter the value);
scanf(%d,&value);
recursion(value);
getch();
void recursion(int x){
If(x==0)
printf(stop function execution \n);
break;
else
printf(\n good morning);
recursion(x-1);// recuesive function call made here .
Output:-
Above program shows the five times good morning
message. because first time function call and jumb of function
definition then consider the value ofX is 5 then compare x value
with 0 in function definition then condition is false and execute
the else part of function and print good morning message then
execute the statement recursion (x-1)then x value is 4 and
function again jump on function definition and compare x value
with 0 then again condition is false print good morning message
this type of iterative work done whenever x value become a
zero. When x value is 0 then if condition true and give the
message stop function execution and stop the execution of
program.
7.3 Solved program of function
Finding the area and circumference of circle
using the function
#include<stdio.h>
#include<conio.h>
void find(int);
void main()
int radius;
clrscr();
printf(enter the radius of the circle);
scanf(%d,&radius);
find(radius);
getch();
voidfind(int)
{
float area;
printf(\n inside the function);
area=3.14*r*r;
circum=2*3.14*r;
printf(\n the area of the circle %2.f,area);
printf(\n the circumference of the circle is %2f,circum);
Output:
Program for return the greatest element
from the array
#include<stdio.h>
#include<conio.h>
int findgreat(int *);
void main()
int num[10],i,gr;
clrscr();
printf(\n enter the elements of the array\n);
for(i=0;i<10;i++)
scanf(%d,&num[i]);
gr=finfgreat(num);
printf(\n the greatest elements of the array is %d,gr);
getch();
}
int findgreat(int *ptr)
int i,great;
great=*ptr;
for(i=1;i<10;i++)
{
ptr++;
if(*ptr>great)
great=*ptr;
}
return great;
Output:
Program finding the cube using the
function.
#include<conio.h>
#include<stdio.h>
void cube(int x)
void main()
{
int num,cube of num;
clrscr();
printf(enter the number);
scanf(%d,&num);
cubeofnum=cube(num);
printf(\n the cube of the number is %d,cubeofnum);
getch();
int cube (int x)
int y;
y=x*X*X;
printf(\n inside the function);
return y;
Returning factorial from the function.
#include<stdio.h>
#include<conio.h>
long findfact(int);
void main()
int num,i;
long fact;
clrscr();
printf(\n enter the number);
scanf(%d,&num);
fact=findfact(num);
printf(\n the factorial of %d is %ld,num,fact);
getch();
long findfact(int num)
int i;
long fact=1;
printf(\n inside the function);
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
Output:
Program finding the sum and average using
the function
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,total;
float average;
clrscr();
printf(\n marks of 5 subjects);
scanf(%d %d %d %d %d
,&m1,&m2,&m3,&m4,&m5);
getsum_avg(m1,m2,m3,m4,m5,&total,&average);
printf(\n the sum is %d,total);
printf(\n the average.is %f,average);
*s=m1+m2+m3+m4+m5;
*a= *s/5.0;
Finding the raise to power of number
#include<stdio.h>
#include<conio.h>
getapower(int x);
void main()
int num1,num2,power;
clrscr();
printf(enter any two number);
scanf(%d%d,&num1,&num2);
power=getpower(num1,num2);
printf(\n num1 raise to power num2 is %d,power);
getch();
}
int getpower (int num,int pow){
int i,z=1;
for(i=0;i<pow;i++)
z=z*num;
return z;
Finding the alternate number up to the
given range using the recursive function.
#include<stdio.h>
#include<conio.h>
int add(int n);
void main()
int num,sum;
clrscr();
printf(\n enter the number);
scanf(%d,&num);
sum-add(num);
printf(\n the sum of the number is %d,sum);
getch();
int add(int n)
int sum;
if(n==0)
return 0;
else
if(n==1)
return 1;
sum=n+add(n-2);
return sum;
Output:
8. STRUCTURE
Structure is a user defined data type which is used to store the
different datatype in single variable structure cover the drawback of array
means array cannot store the different datatype in single variable.
How to use the structure of program
If we want to use the structure in program for that c language provides
the struct Keyword to us.
Syntax:- struct structure name
Datatype variablename;
Datatype variablename;
Datatype variablename;
};
e.g. struct student {
int id;
char name[80];
float marks;
};
If i declare the structure in program but I want to use the structure
member in program.
Then what can i do.
For that we need to create the variable of type structure or variable
that structure.
What is role of structure variable in program
If we declare then structure not allocates memory means structure
member not stored in memory.but if we create the variable of that structure
then structure member stored in memory.after that we can use it in program.
How create the variable of structure
Struct structurename variablename;
e.g. struct student st;// st is the variable of structe we can use the
member of structure through st.
or
another way of structure variable creation.
Struct structure name
Datatype variable name;
Datatype variable name;
Datatype variable name;
}
Variable1,variable2;
e.g. struct student
Int id;
Char name [80];
Float marks;
St,st1;
How to access the member of structure through the structure
variable
Syntax:- structvariable member of structure;
e.g. st id=100;
() dot is the member access operator which is used to access the
structure member outside of structure anywhere in programme.
Memory representation given as per above example
Id
name marks
Integer String Float
value can value can value can
store store here store here
100 102
182
Above program shows the memory area of st variable
Because every character is 1 byte and its size is 80 and last variable
allocate 4 byte memory means overall size of st is 186 byte long.
Steps for structure use
1] Declare the structure
2] Create its variable
3] Access its member using structure variable member of structure.
8.1 Program demonstrate the how to use the
structure in program
#include<stdio.h>
#include<conio.h>
void main()
struct student {
int id;
char name[80];
float marks;
};
struct student st;// create variable of type.
clrscr();
printf(enter the name of student \n);
scanf(%s,&st.name);//access name from structure
printf(enter the id of student \n);
scanf(%d,&st.id);//access id from structure
printf(enter the marks of student\n);
scanf(%f,&st.marks);
printf(name is %s \n,st.name);
printf(id is %d\nst.id);
printf(marks is %f\n,st.marks);
getch();
Output:
In above example we cannot store the more than one structure data in
single variable if we required more than one student data then we need to
create the variable of structure as per your requirement so your programme
length increase unnecessary for that we can create the Array of structure.
8.2 Array of structure
If we create the array of structure variable then we can store the more
than one structure data in single variable.
How to create of array of the structure.
Syntax:- struct structurename variablename[size];
e.g. struct student st[5];
here the st is a variable of structure it will allocate the memory of
five structure.
Demonstrating the array of structure
#include<stdio.h>
#include<conio.h>
struct student
char name[20]; int roll,age; };
main()
int i; struct student S[10];
clrscr();
for(i=0;i<3;i++)
printf("\nEnter the details of the student : ");
scanf("%s %d %d",S[i].name,&S[i].roll,&S[i].age);
clrscr();
printf("\nThe details of the student are\n");
for(i=0;i<3;i++)
{ printf("\n%s \n%d
\n%d\n\n",S[i].name,S[i].roll,S[i].age);
getch(); return 0;
output:-
What is the problem with above problem if we use the array then we
cannot give the more data than specified aize and if we give the less
element than size of array then remaining memory will not use.So we need
to create the dynamic memory allocation with structure means we need to
use the pointer with structure.
8.3 Program demonstrate use of dynamic
memory allocation with structure
#include<stdio.h>
#include<conio.h>
Struct student
char name[20];
int roll,age;
};
void main()
Struct student *sptr;
clrscr();
sptr=(struct student *)malloc( sizeof (struct student));
printf(\n enter the details of the student :);
scanf(%S%d%d,&sptr->name,&sptr->roll,&sptr-
>age);
Printf(\n %s\n%d\n%d\n,sptr->name,sptr->roll,sptr-
>age);
getch();
Output:
How to pass the structure in function
Syntax:- returntype functionname(struct structurename);
e.g. void accept(struct student);
if we want to pass the structure in function for that we need to declare
structure at globally
8.4 Program demonstrate function in structure
#include<stdio.h>
#include<conio.h>
struct student
{ int id,char,name[90];
float marks;};
struct student st;
void main()
void accept(struct student );
clrscr();
accept(st);//pass all structure member from here
getch();
void accept(struct student st){
printf(enter the name of student\n);
scanf(%s,&st.name);
printf(enter the id of student \n);
scanf(%d,&st.id);
printf(enter the marks of student \n);
pcanf(%f,&st.marks);
printf(name is %s ,&st.name);
printf(id is %d,&st.id);
printf(marks %f,&st.marks);
Output:-
8.5 Demonstrating the structure array variable
#include<stdio.h>
#include<conio.h>
struct book
char name[20], author[20];
int price;
B,B1,B2;
main()
struct book B; clrscr();
printf("Enter the name,author and the price of the book :
");
scanf("%s %s %d",B.name,B.author,&B.price);
printf("The details of book");
printf("\nName = %s",B.name); printf("\nAuthor =
%s",B.author);
printf("\nPrice = %d",B.price); getch();
return 0;
output:-
Demonstrating the structure array
#include<stdio.h>
#include<conio.h>
struct customer
{char name[20]; int age,acc_no; int balance;};
main()
struct customer C[10],C1; int deposit,i;
clrscr();
for(i=0;i<3;i++)
printf("\nEnter the name,age,acc.no. and balance of
customer : ");
scanf("%s %d %d
%d",C[i].name,&C[i].age,&C[i].acc_no,&C[i].balance);
printf("\nEnter the amount to be deposited : ");
scanf("%d",&deposit);
C[i].balance += deposit;
for(i=0;i<3;i++)
printf("\n\nthe details of customer %d",i+1);
printf("\nName = %s \nAge = %d \nAcc. No = %d
\nBalance = %d",C[i].name,C[i].age,C[i].acc_no,C[i].balance);
getch();
return 0;
}
output:-
8.6 Passing and returning the structure variable
from the function
#include<stdio.h>
#include<conio.h>
struct customer
{char name[20]; int age,acc_no; float balance;};
struct customer modify(struct customer);
main()
struct customer C;
clrscr();
printf("\nEnter the name,age,acc.no. and balance of
customer : ");
scanf("%s %d %d
%f",C.name,&C.age,&C.acc_no,&C.balance);
printf("\nThe details of customer ");
printf("\nName = %s \nAge = %d \nAcc. No = %d
\nBalance = %.2f",C.name,C.age,C.acc_no,C.balance);
C = modify(C);
printf("\nAfter depositing the amount the balance is
%.2f",C.balance); getch(); return 0;
struct customer modify(struct customer x)
int deposit; printf("\nEnter the amount to be deposited :
");
scanf("%d",&deposit); x.balance += deposit; return x;
Output:-
8.7 Demonstrating nesting of structure.
Nesting of structures
#include<stdio.h>
#include<conio.h>
struct phone
int std; long local;};
struct address
int plotno; char city[20]; struct phone P;
};
struct bdate {int date,year; char month[10];};
struct student {char name[20]; int roll,age; struct
address A; struct bdate B;};
main()
struct student S; clrscr(); printf("\nEnter the name roll
and age of the student : ");
scanf("%s %d %d",S.name,&S.roll,&S.age);
printf("\nEnter the plot number and the city : ");
scanf("%d %s",&S.A.plotno,S.A.city); printf("\nEnter
the date, month and the year : ");
scanf("%d %s
%d",&S.B.date,S.B.month,&S.B.year);printf("\nEnter the std
code and the local number ");
scanf("%d %ld",&S.A.P.std,&S.A.P.local);
printf("\nThe details of the studemt are \n ");
printf("\n%s \n%d
\n%d\n%d\n%s",S.name,S.roll,S.age,S.A.plotno,S.A.city);
printf("\nDate = %d Month = %s Year =
%d",S.B.date,S.B.month,S.B.year);
printf("\nThe std code and the local number are ");
printf("%d - %ld",S.A.P.std,S.A.P.local);
getch();
return 0;
Output:-
8.8 Demo of pointer variable of structure
#include<stdio.h>
#include<conio.h>
struct student
char name[20]; int roll,age; }*sptr;
main()
struct student S; sptr = &S; clrscr(); printf("\nEnter
the details of the studemt ");
scanf("%s %d %d",sptr->name,&sptr->roll,&sptr-
>age); printf("\nthe details of the studemt are \n ");
printf("\n%s \n%d \n%d\n",sptr->name,sptr->roll,sptr-
>age); getch(); return 0;
Output:-
9. UNION
Union is also one kind of data type like structure. An union is variable
that may hold (at different times) objects of different types and size, with the
compiler keeping track of size and alignment requirements. Union provides
a way to manipulate different kind of data in single area of storage an union
will contain one of the may different types of value (as long as only one is
stored at a time).
The declaration and the usage of union is same as structure union hold
only one value for one datatype (if we new assignment is made the previous
value is automatically erased).
Declaring an union is similar to declaring structure. The general form
of union is.
Syntax:- union unionname {
Type number name 1;
Type number name2;
Type number name;
};
Union union name variable name;
9.1 Program demonstrate the use of union
#include<stdio.h>
#include<conio.h>
void main()
union student
int id;
char name[80];float marks;
union student st;//create variable at type.
printf(enter the name of student \n);
scanf(%s,&st.name);//access name from structure.
printf(enter the id of student\n);
scanf(%d,&st.id);
printf(enter the marks of student\n);
scanf(%f,&st.marks);
printf(name is %s \n,st.name);
printf(id is %d\n,st.id);
printf(marks is %d\n ,st.marks);
getch();
Output:-
Name is anil
Id is 2
Marks is 90
10. STRING HANDLING IN C
String means collection of more than one character, string is used to
store the more than one character alphabetical data.
e.g. if we want to give the name, address, type data in application for
that we need to use the string.
How to declare the string or how to create the string
program
Syntax:- char variable name[size];
e.g. char name[5];//this declaration says name can store the 5
character data in it.
Memory representation of string
0 1 2
3 4
G I R I \0
100 101 102
103 104
\0 Indicate to us end of string. The character insert automatically by
compiler
How to input the string from scanf()for that use the %s format
specifier
Scanf(%s,string);
e.g. scanf(%s,name);
Following program shows accessing with
scanf () function
#include<stdio.h>
#include<conio.h>
void main()
char name [80];
clrscr();
printf(enter the name of student \n);
scanf(%s,name);
printf(name is %s\n,name);
getch();
}
Output:
10.1 Demonstrating the string array
#include<stdio.h>
#include<conio.h>
main()
int i; char name[5][10]; clrscr();
for(i=0;i<5;i++) { printf("\nEnter the string : ");
scanf("%s",name[i]); }
printf("\nThe strings are \n");
for(i=0;i<5;i++) { printf("\n%s",name[i]); }
getch(); return 0; }
output:-
10.2 Use of gets() and puts() function
#include<stdio.h>
#include<conio.h>
void main()
char name[90];
clrscr();
puts(enter the name of student );
gets(name);
puts (name);
getch();
This program print the name with space because gets() has capability
to print the string with name.
10.3 STRING HANDLING FUNCTION
String handling function are used to perform the operation on string.
Following are the string handling function.
Strcpy():- the function is used to copy the one string to the another
string.
strcmp():- the function is used to perform the comparison between
two string.
Strlen():- this function is used to find the length of string.
Strcat():- this function is used to combine the two string.
Strupr():- this function is used to convert the lower case string to the
upper case string.
Strlwr():- this function is used to convert the uppercase string in to
the lower case .
Strrev():-this function is used to reverse the string.
11. FILE HANDLING
11.1 What is a File?
A file is a collection of related data that a computers treats as a
single unit.
Computers store files to secondary storage so that the contents
of files remain intact when a computer shuts down.
When a computer reads a file, it copies the file from the storage
device to memory; when it writes to a file, it transfers data from
memory to the storage device.
C uses a structure called FILE (defined in stdio.h) to store the
attributes of a file.
11.2 Steps in Processing a File
1. Create the stream via a pointer variable using the FILE
structure:
FILE *p;
2. Open the file, associating the stream name with the file name.
3. Read or write the data.
4. Close the file.
11.3 The basic file operations
fopen - open a file- specify how its opened (read/write) and
type (binary/text)
fclose - close an opened file
fread - read from a file
fwrite - write to a file
fseek/fsetpos - move a file pointer to somewhere in a file.
ftell/fgetpos - tell you where the file pointer is located.
11.4 File Open Modes
More on File Open Modes
Additionally,
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)
File Open
Syntax:
filepointer=fopen(filename, mode);
The file open function (fopen) serves two purposes:
It makes the connection between the physical file and the
stream.
It creates a program file structure to store the
information C needs to process the file.
More On fopen
The file mode tells C how the program will use the file.
The filename indicates the system name and location for the
file.
We assign the return value of fopen to our pointer variable:
spData = fopen(MYFILE.TXT, w);
spData = fopen(A:\\MYFILE.TXT, w);
More On fopen
Closing a File
When we finish with a mode, we need to close the file before
ending the program or beginning another mode with that same
file.
To close a file, we use fclose and the pointer variable:
fclose(spData);
fprintf():-
Syntax:
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(out.txt,w);
fprintf (fp, "%d %f %c", i, x, ch);
fscanf():-
Syntax:
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(input.txt,r);
int i;
fscanf (fp,%d",i);
getc():-
Syntax:
identifier = getc (file pointer);
Example:
FILE *fp;
fp=fopen(input.txt,r);
char ch;
ch = getc (fp);
putc():-
write a single character to the output file,
pointed to by fp.
Example:
FILE *fp;
char ch;
putc (ch,fp);
End of File
There are a number of ways to test for the end-of-file
condition. Another way is to use the value returned by the
fscanf function:
FILE *fptr1;
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == feof(fptr1) )
printf ("End-of-file encountered.\n) ;
Reading and Writing Files
#include <stdio.h>
int main ( )
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;
outfile = fopen ("testdata", "w") ;
fprintf (outfile, %f %d %f ", a, b, c) ;
fclose (outfile) ;
infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;
printf ( %f %d %f \n ", a, b, c) ;
printf ( %f %d %f \n ", e, f, g) ;
Example
#include <stdio.h>
#include<conio.h>
void main()
char ch;
FILE *fp;
fp=fopen("out.txt","r");
while(!feof(fp))
ch=getc(fp);
printf("\n%c",ch);
getch();
fread ():-
Declaration:
size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
Remarks:
fread reads a specified number of equal-sized
data items from an input stream into a block.
ptr = Points to a block into which data is read
size = Length of each item read, in bytes
n = Number of items read
stream = file pointer
Example:
#include <stdio.h>
int main()
FILE *f;
char buffer[11];
if (f = fopen("fred.txt", r))
fread(buffer, 1, 10, f);
buffer[10] = 0;
fclose(f);
printf("first 10 characters of the file:\n%s\n", buffer);
return 0;
fwrite():-
Declaration:
size_t fwrite(const void *ptr, size_t size, size_t n,
FILE*stream);
Remarks:
fwrite appends a specified number of equal-sized data
items to an output file.
ptr = Pointer to any object; the data written begins
at ptr
size = Length of each item of data
n =Number of data items to be appended
stream = file pointer
Example
Example:
#include <stdio.h>
int main()
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);
return 0;
fseek():-
This function sets the file position indicator for the stream
pointed to by stream or you can say it seeks a specified place
within a file and modify it.
SEEK_SET Seeks from beginning of file
SEEK_CUR Seeks from current position
SEEK_END Seeks from end of file
Example:
#include<stdio.h>
intmain()
{
FILE * f;
f = fopen("myfile.txt", "w");
fputs("Hello World", f);
fseek(f, 6, SEEK_SET); SEEK_CUR,
SEEK_END
fputs(" India", f);
fclose(f);
return 0;
}
ftell():-
offset = ftell( file pointer );
"ftell" returns the current position for input or output
on the file
#include <stdio.h>
int main(void)
FILE *stream;
stream = fopen("MYFILE.TXT", "w");
fprintf(stream, "This is a test");
printf("The file pointer is at byte %ld\n",
ftell(stream));
fclose(stream);
return 0;