Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CHARACTER SET OF C
2. VARIABLE
3. OPERATORS
3.6Conditional operator
3.7 Programs
6.1 Arrays
6.3 Pointer
7. FUNCTION OF C LANGUAGE
7.1 Types of function
8. STRUCTURE
8.6 Passing and returning the structure variable from the function
9. UNION
Alphabet: a to z or A to Z
Digit: 0 to 9
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
Types of constant
Numeric Constant
1] Integer Constant:-
3] Character Constant:-
In character Constant we can store the single character constant and more
than one character constant.
e.g.a[]={good};
Keywords 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.
A] Integer data type:-this data type indicates to us only use the value without
decimal point.
i] Integer
ii]Short Integer
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.
i]Float:-
ii]Double:-
Double datatype can store the value -1.7x10 e 308 to 1.7x 10e308
iii]Long Double:-
Long double datatype can store the value -1.7 x 10e4932 to 1.7 x
10e4932
2] Unsigned Character
a]Derived data type:- Those datatype we can use with any other 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.
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.
Variable also used to retrieve the value from memory for display
the output for some process.
E.g.int a,b,c;
Variable means name start with alphabet cannot start with digit or
any special Symbol other than underscore character.
e.g.
e.g.
e.g.
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>
Returntype main()
printf(good morning);
getch();
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
printf(good morning.);
getch();
Output:
Input function:-
Gets():- this function is used to accept the string output from keyboard.
Puts():-this function is used to display the string output on monitor
Scanf():-this function is used to read the any type of data from keyboard.
Charactervariable=getchar();
Syntax of putchar():-
Putchar(character variable);
e.g. putchar(ch);
#include<stdio.h>
#include<conio.h>
void main()
char ch;
clrscr();
ch=getchar();
putchar(ch);
getch();
Output:
e.g. printf(%d,c);
e.g. scanf(%d,&a);
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
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.
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
Syntax : exp1:exp2:exp3;
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
#include<stdio.h>
#include<conio.h>
void main()
int radius ;
float area,circum;
clrscr();
float pi=3.14;
scanf(%d,&radius);
area=pi*radius*radius;
circum=2*pi*radius;
Output:-
#include<stdio.h>
#include<conio.h>
void main()
int items;
clrscr();
scanf(%f,&price);
bill=items*price;
discount=0.2*bill;
bill=bill-discount ;
getch();
Output:
#include<conio.h>
void main()
int x=10,y=10,float z;
clrscr();
z=x+y;
x=x+y;
y+=z;
x++;
y=x++;
z=++y;
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int num,rem,sum=0;
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;
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.
1] Simple if statement
2] If else statement
4] Nested if statement
5] Switch statement
Syntax:- if(condition)
if(a>b)
If(b>a)
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(%d %d,&a,&b);
if(a>b)
if(b>a)
getch();
}
Output:-
4.2 How to use the if else statement
If (condition)
Else
#include<stdio.h>
#include<conio.h>
void main()
int no;
printf(enter the number from keyboard);
scanf(%d,&no);
if(no%2==0)
else
getch();
Output:-
4.3 How to use the nested if statement
Syntax:-
if (condition)
if(condition)
Else
else
{
statement block
#include<stdio.h>
#include<conio.h>
void main()
int id,sal;
clrscr();
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
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)
else if (condition )
else
}
Other program part
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(%d%d%d,&a,&b,&c);
if(a>b&&a>c)
else if(b>a&&b>c)
{
Else
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
}
#include<stdio.h>
#include<conio.h>
void main()
int choice;
clrscr();
scanf(%d,&choice);
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
getch();
Output:-
4.6 Solved programs for decision making statement
#include<stdio.h>
#include<conio.h>
void main()
int units;
float bill;clrscr();
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;
getch();
Output:
#include<conio.h>
void main()
int items;
float discount ,bill,price;
clrscr();
scanf(%d,&items);
scanf(%f,&price);
bill=items*price;
if(bill>1000)
discount =bill*0.1;
bill=bill-discount;}
else
if(bill>5000)
discount =bill*0.1;
bill=bill-discount;
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
int num;
clrscr();
scanf(%d,&num);
if(num>0)
if(num<0)
if(num==0)
getch();
Output:-
#include<stdio.h>
#include<conio.h>
void main()
int items;
float discount,bill,price;
clrscr();
scanf(%d,&items);
scanf(%d,&price);
bill=items*price;
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
getch();
Output:-
5. CONTROL STATEMENT OR
LOOPING STATEMENT
Control statements are use if we want to perform the some task
repetitively again and again.
If we want to use the any type of loop we need three important steps.
Output:-
Good morning
Good morning
Good morning
Good morning
Good morning
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.
#include<stdio.h>
#include<conio.h>
void main()
int no,table,I;
clrscr();
scanf(%d,&no);
for(i=1;i<=100;i++)
{
table=no*I;
printf(%d\n,table);
getch();
Output:-
Syntax:- Initialization ;
While(condition)
{
while(i<=5)
Printf(good morning);
I++;
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();
scanf(%d,&no);
i=1;
while(i<=no)
{
table =i*no;
printf(%d\n,table);
i++;
getch();
}
#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
#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:-
#include<stdio.h>
#include<conio.h>
void main()
int marks1,marks2,marks3,sum;
char choice;
clrscr();
do
scanf(%d%d%d,&marks1,&marks2,&marks3);
sum=marks1+marks2+marks3;
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();
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();
scanf(%d,&num);
for(i=2;i<num/2;i++)
rem=num%i;
if(rem==0)
break;
}
}
if(i==(num/2))
getch();
Output:-
#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);
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf(*\t);
printf(\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;
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:-
#include<stdio.h>
#include<conio.h>
void main()
int num,sum,temp,rem,limit;
clrscr();
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:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,temp,rem;
clrscr();
scanf(%d,&num);
temp=num;
while(num>0)
rem=num%10;
sum=sum+rem*rem*rem;
num/=10;
if(sum==temp)
else
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
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
The following diagram shows how array stored in memory as per above
example.
0 1 2
3 4-Index
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.
Scanf(%d,&array variable(variable));
e.g.
for(i=0;i<5;i++)
Scanf(%d,&a[i]);
For(variable=0;variable<size of array;variable++)
Printf(%d,arrayvariable(variable));
e.g. for(i=0;i<5;i++)
Printf(%d,a[i]);
}
#include<conio.h>
void main()
int a[5],i;
clrscr();
for(i=0;i<5;i++)
scanf(%d,&a[i]);
for(i=0;i<5;i++)
printf(a[%d]------%d\n,I,a[i]);
}
getch();
Output:-
e.g.int a[3][4];
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]);
{
for(j=0;j<3;j++)
scanf(%d,&a[i][j]);
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
#include<stdio.h>
#include<conio.h>
void main()
int num1[10],num2[10],sum[10],i,j,k;
clrscr(();
for(i=0;i<10;i++)
scanf(%d,&num1[i]);
for(i=0;i<10;i++)
scanf(%d,&num2[i]);
sum[i]=num1[i]+num2[i];
printf(\n%d,sum[i]);
getch();
Output:-
#include<stdio.h>
#inlclude<conio.h>
void main()
int num[10],i,pos=0,neg=0,even=0,odd=0,zero=0;
clrscr();
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++;}
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();
scanf(%d,&num[i]);
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();
scanf(%d,&num);
while(num>0)
{
binary[i]=num%2;
num=num/2;
i++;
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();
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;
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]);
}
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.
e.g. a=&b;
#include<conio.h>
void main()
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
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.
Int **ptr.*a,b;
To store the two address field because ptr having two star(indirection
pointer).
#include<stdio.h>
#include<conio.h>
void main(){
int **ptr,*a,b=10;
a=&b;
ptr=&a;
getch();
Output:
How to create the dynamic memory allocation using the pointer
Syntax:-pointervariable=(datatype*)malloc(sizeof(size));
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));
scanf(%d,&ptr);
getch();
getch();
}
Output:
#include<conio.h>
void main()
int *ptr,i,size;
clrscr();
scanf(%d,&size);
for(i=0;i<size;i++)
{
scanf(%d,ptr);
ptr++;
ptr=ptr-size;
getch();
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.
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
e.g. printf(),scanf()etc.
ii] Function calling:- function call means execute or run the body of
function or definition function calling generally use for reuse the function
definition.
Function name(variable);
e.g.add(a,b);
Here add cannot return the value if add() function return type is void.
Body of function
Return(x+y);
}
Note:- cannot define function in main function this is the rule of c
language
#include<stdio.h>
#include<conio.h>
void main()
int no,sq;
clrscr();
scanf(%d,&no);
sq=squre(no);
getch();
}
Int squre(int x)
Return(x*x);
Output:-
#include<stdio.h>
#include<conio.h>
void main(){
void squre();
clrscr();
squre();
getch();
void squre ()
{
int no;
scanf(%d,&no);
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int squre();
clrscr();
getch();
void squre ()
int no;
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.
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(%d %d,&a,&b);
swap(a,b);
printf(A=%d\t B=%d\n,a,b);
getch();
temp;
x=y;
y=temp;
printf(X=%d\t Y=%d\n,x,y);
Output:-
X=20 y=10
X=10 y=20
If we swap the values in definition not effect on calling function
argument.
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
clrscr();
scanf(%d%d,&a,&b);
swap(&a,&b);
printf(A=%d\t B=%d\n,a,b);
getch();
temp=*x;
*x=*y;
*y=temp;
printf(X=%d\t Y=%d\n,x,y);
X=10 y=20
A=20 B=10.
FUNCTION RECURSION
OR
Call same function within its definition is called as
function recursion.
#include<stdio.h>
#include<conio.h>
void main()
void recursion(int);
int value;
clrscr();
scanf(%d,&value);
recursion(value);
getch();
If(x==0)
printf(stop function execution \n);
break;
else
Output:-
#include<stdio.h>
#include<conio.h>
void find(int);
void main()
int radius;
clrscr();
scanf(%d,&radius);
find(radius);
getch();
voidfind(int)
{
float area;
area=3.14*r*r;
circum=2*3.14*r;
Output:
#include<stdio.h>
#include<conio.h>
int num[10],i,gr;
clrscr();
for(i=0;i<10;i++)
scanf(%d,&num[i]);
gr=finfgreat(num);
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:
#include<conio.h>
#include<stdio.h>
void cube(int x)
void main()
{
clrscr();
scanf(%d,&num);
cubeofnum=cube(num);
getch();
int y;
y=x*X*X;
return y;
#include<conio.h>
long findfact(int);
void main()
int num,i;
long fact;
clrscr();
scanf(%d,&num);
fact=findfact(num);
getch();
int i;
long fact=1;
fact=fact*i;
return fact;
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,total;
float average;
clrscr();
scanf(%d %d %d %d %d
,&m1,&m2,&m3,&m4,&m5);
getsum_avg(m1,m2,m3,m4,m5,&total,&average);
*s=m1+m2+m3+m4+m5;
*a= *s/5.0;
#include<stdio.h>
#include<conio.h>
getapower(int x);
void main()
int num1,num2,power;
clrscr();
scanf(%d%d,&num1,&num2);
power=getpower(num1,num2);
getch();
}
int getpower (int num,int pow){
int i,z=1;
for(i=0;i<pow;i++)
z=z*num;
return z;
#include<stdio.h>
#include<conio.h>
int add(int n);
void main()
int num,sum;
clrscr();
scanf(%d,&num);
sum-add(num);
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.
Datatype variablename;
Datatype variablename;
Datatype variablename;
};
int id;
char name[80];
float marks;
};
If i declare the structure in program but I want to use the structure
member in program.
e.g. struct student st;// st is the variable of structe we can use the
member of structure through st.
or
Variable1,variable2;
Int id;
Float marks;
St,st1;
e.g. st id=100;
Id
name marks
Integer String Float
value can value can value can
store store here store here
100 102
182
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.
#include<stdio.h>
#include<conio.h>
void main()
struct student {
int id;
char name[80];
float marks;
};
clrscr();
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.
#include<stdio.h>
#include<conio.h>
struct student
main()
for(i=0;i<3;i++)
scanf("%s %d %d",S[i].name,&S[i].roll,&S[i].age);
clrscr();
{ 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()
clrscr();
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:
#include<stdio.h>
#include<conio.h>
struct student
{ int id,char,name[90];
float marks;};
void main()
clrscr();
getch();
scanf(%s,&st.name);
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
int price;
B,B1,B2;
main()
scanf("%s %s %d",B.name,B.author,&B.price);
printf("The details of book");
output:-
#include<stdio.h>
#include<conio.h>
struct customer
main()
for(i=0;i<3;i++)
scanf("%s %d %d
%d",C[i].name,&C[i].age,&C[i].acc_no,&C[i].balance);
scanf("%d",&deposit);
C[i].balance += deposit;
for(i=0;i<3;i++)
getch();
return 0;
}
output:-
8.6 Passing and returning the structure variable
from the function
#include<stdio.h>
#include<conio.h>
struct customer
main()
struct customer C;
clrscr();
scanf("%s %d %d
%f",C.name,&C.age,&C.acc_no,&C.balance);
C = modify(C);
Output:-
8.7 Demonstrating nesting of structure.
Nesting of structures
#include<stdio.h>
#include<conio.h>
struct phone
struct address
};
main()
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);
getch();
return 0;
Output:-
8.8 Demo of pointer variable of structure
#include<stdio.h>
#include<conio.h>
struct student
main()
scanf("%s %d %d",sptr->name,&sptr->roll,&sptr-
>age); printf("\nthe details of the studemt are \n ");
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).
};
#include<stdio.h>
#include<conio.h>
void main()
union student
int id;
scanf(%d,&st.id);
scanf(%f,&st.marks);
printf(name is %s \n,st.name);
printf(id is %d\n,st.id);
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.
0 1 2
3 4
G I R I \0
100 101 102
103 104
How to input the string from scanf()for that use the %s format
specifier
Scanf(%s,string);
e.g. scanf(%s,name);
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf(%s,name);
printf(name is %s\n,name);
getch();
}
Output:
10.1 Demonstrating the string array
#include<stdio.h>
#include<conio.h>
main()
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();
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.
Strcpy():- the function is used to copy the one string to the another
string.
Strupr():- this function is used to convert the lower case string to the
upper case string.
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.
2. Open the file, associating the stream name with the file name.
Additionally,
r+ - open for reading and writing, start at beginning
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.
More On fopen
Closing a File
fprintf():-
Syntax:
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(out.txt,w);
fscanf():-
Syntax:
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(input.txt,r);
int i;
fscanf (fp,%d",i);
getc():-
Syntax:
Example:
FILE *fp;
fp=fopen(input.txt,r);
char ch;
ch = getc (fp);
putc():-
Example:
FILE *fp;
char ch;
putc (ch,fp);
End of File
FILE *fptr1;
int istatus ;
if ( istatus == feof(fptr1) )
#include <stdio.h>
int main ( )
int b = 5, f ;
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:
Remarks:
Example:
#include <stdio.h>
int main()
FILE *f;
char buffer[11];
if (f = fopen("fred.txt", r))
buffer[10] = 0;
fclose(f);
return 0;
fwrite():-
Declaration:
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.
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():-
#include <stdio.h>
int main(void)
FILE *stream;
fclose(stream);
return 0;