Cprogramming Notes
Cprogramming Notes
TYPE OF COMPILERS
1.turboc
2.boroland-c
3quick c
4.turboc2
DATA TYPES :-
1.BIT = 1 CHARACTER
8 BIT = 1 BYTE
1024 BYTES = 1 KILO BYTES
1024 KB = 1 MEGA BYTE
1024 MB = 1 GIGA BYTE
1024 GB = 1 TERRA BYTES
1024 TB = 1 HEXA BYTE
1024 HB = 1 ZETA BYTE
Start---- Run----cmd---ok
I. Designer window
Bti
OUTPUT STATEMENT :-
Printf(“control string”,arg1,arg2,……………….argn”);
Printf(“%d%f%c”,sum,avg,place);
Syntax :-
Printf(“any massages”);
Example :-
Printf(“Hello”);
INPUT STATEMENT :-
Syntax :- used to read value for the variables in a program from the keyboard.
Used to accept numeric, character &string type of data.
It is the address of memory location where the values of input variables should be
stored.
Scanf(“format specified”,& variable 1,& variable 2);
Example:- scanf(“%d”,&a);
STRUCTURE OF C- PROGRAMMING
Header file :-
#include<stdio.h>
#include<conio.h>
Main function ( )
{
Data type and variable declaration
c-statement-1
c-statement-2
c-statement-n
}
ESCAPE SEQUENTIAL CHARACTER :-
1.”\n” new line or next line
2. “\t” Horizantal tab space
3.”\a” Beep.
Bti
Write a program to display the given massage as a output using print statement in c-
languages
1. #inclued<stdi.o>
#inclued<conio.h>
Void main()
{
Clrscr();
Printf(“welcome to c-language\n”);
Printf(“this is a sample program”);
Getch();
}
OUTPUT
Welcome to c-language
This is a sample program
2. #inclued<stdi.o>
#inclued<conio.h>
Void main()
{
Clrscr();
Getch();
}
OUTPUT
Bhagyashree computer vazirbad nanded
Bti
VARIABLE DECLARATION RULE :-
2. the variable name should not stands with number (or)any special symbol it
4.the variable name should be a single word we can not create space between
variable
OPERATORS :-
An operators is a symbol that tells the computer to perform certain mathematical or
logical manipulation (calculations)
Computer acts as connectors and they indicate what type of operation is being
carried
The value on which operator perform operation is called operand.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Bti
Increment and decrement operators
Conditional oprators
1. ARITHMETIC OPERATORS:-
OPERATORS OPERATION
+ Addition
__ subtraction
* Multiplication
/ Division (given quotient)
% Modulus (gives remainder)
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c,d,e;
clrscr();
a=20;
b=30;
c=40;
d=a+b+c;
e=a*b*c;
getch();
Bti
output
/*Write a program to read two number then calculate additional using c- language *\
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
a=20;
b=30;
c=a+b;
getch();
/*Write a program to read three subject mark then calculate total marks &
average*/
#include<stdio.h>
#include<conio.h>
void main()
int m,p,c,total
float avg;
clrscr();
m=20;
p=30;
c=40;
total =a+b+c;
avg =total/3;
printf("your total marks are=%d",total);
Bti
printf("your average is =%f",avg);
getch();
}
Write a program to read two number then calculate addition using c- language
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
scanf(“%d%d”,&a,&b);
c=a+b;
getch();
Write a program to read three subject mark then calculate total marks & average
(at the run time storing)
#include<stdio.h>
#include<conio.h>
void main()
{
int m,p,c,total
float avg;
clrscr();
printf(“Enter your three subject marks”);
scanf(“%d%d%d”,&m&p&c);
total =a+b+c;
avg =total/3;
Bti
printf("your total marks are=%d",total);
printf("your average is =%f",avg);
getch();
}
#include<stdio.h>
main()
{
float basicsal,da,hra,pf,grosssal,netsal;
clrscr();
printf("Enter basic salary of the employee: Rs.");
scanf("%f",&basicsal);
da=(basicsal*25)/100;
hra=(basicsal*15)/100;
pf=(basicsal*10)/100;
grosssal=basicsal+da+hra+pf;
netsal=grosssal-pf;
Bti
RELATIONAL OPERATORS :
Operators Operation
== Equal to
!= not Equal to
#include <stdio.h>
Main()
{
Int p=10,q=20, r=10,a,b,c,d;
a=(p>=q);
b=(p<q);
c=(p==q);
d=(q< r );
printf(“a=%d\n b=%d\n c=%d\n d=%d”,a,b,c,d);
getch();
}
Bti
Output
a =0
b =1
c=1
d =1
LOGICAL OPRATORS :-
These are used to take decision. These are used to connect one (or)more relational
expression. The result of this operator is either true or false
Non zero value TRUE =1
Zero value FALSE =0
Operation Operator
Logical AND &&
Logical OR ||
Logical not !
TRUTH TABLE
A B
F F
T F
#include <stdio.h>
Main()
{
Int a=10,b=0,c=7,p,q,r;
Clrscr();
P=a&&b||c;
Q=a||b&&c;
R=a&&b&&c;
Bti
Printf(“p=%d\n q=%d\n r=%d”,p,q,r);
Getch();
}
output
p=1
q=1
r=0
ASSINGMENT OPERATOR :-
Operator operation
+= a=a+b
-= a=a-b
*= a= a*b
/= a=a/b
%= a=a%b
#include <stdio.h>
Main()
{
Int a=9,b=5,c=7;
Clrscr();
A+=1;
b-=b;
c*=c;
printf(“a=%d\n b=%d\n c=%d”,a,b,c);
getch();
}
Output
a=10
b=0
c=49
Bti
INCREMENT AND DECREMENT
Pre-Decrement: - -i i=i-1
Post decrement i- - -1
#include <stdio.h>
Main()
Clrscr();
c=++a + ++b;
d=- - a - - - b;
e=a++;
f=a+(++b);
Getch();
Bti
Output
c= 17
d= -1
e= 7
f= 17
Bti
CONDITIONAL OPERATOR
Example :
#include<stdio.h>
Main()
{
Int p=4 ,q=5,min;
Min=(p<q)?p:q;
Printf(“%d\n”,p,q);
Getch();
}
Output
P=4
#include<stdio.h>
Main()
{
Int x=3, y=9 , z=4, p;
P=((x>y)? x:( y>z) ? y:z);
Printf(“the greatest no.is %d’,p);
Getch();
}
Output
P=9
Bti
CONTROL STATEMENT :-
Syntax:-
If(condition)
{
Statement block;
}
Next statement
Syntax :-
{
Block1 of c- statements;
Else
Block 2 of c-statements;
in the above syntax if given all condition proves to be true atometically the
write a program to read maths marks than find out whether student passed or failed
main()
{
Int m;
Clrscr();
Printf(“Enter your math marks”);
Scanf(“%d”,&m);
If(m>=35);
{
Printf(“pass”);
}
Else
{
Printf(“fail”);
}
Getch();
}
Bti
else if /if else-if :-if there are more than one statement and we have to select any one
from the given alternatives then if-else-if is used.
Syntax :- If(condition-1)
{
Statement -1;
Else
{
If(condition-2)
{
Statement -2;
}
Else
If(condition-3)
{
Statement -3;
Else
{
If(condition n)
{
Statement n;
}
Else
{
Statement n;
}
}
}
Note :- conditions are evaluated from top to bottom as soon as a true condition is
found the statement associated with it is execute and the contol is transferred to the
When all the ‘n’ conditions are false then the final else containing the default
Bti
/*Program to read marks of a student and find the division */
#include<stdio.h>
main()
int a,b,c,avg;
clrscr();
scanf("%d%d%d",&a,&b,&c);
else
else
if(a<35&&b<50)
if(a<35)
printf("fail");
}
getch();
}
Bti
If –else statement :-
if there are two statements to be executed alternatively then this conditional statement
is used.
It is also called as two-way branching
Syntax :-
If (condition)
{
Statement 1; (true block )
}
Else
{
Statement 2; (false block)
}
Next statement;
main()
{
int x,y;
clrscr();
printf(" enter any two values\n");
scanf("%d%d",&x,&y );
if(x= =y)
{
Printf(“the numbers are equal”);
}
Else
{
Printf(“the numbers are not equal”);
}
Getch();
}
Output
5
5
The numbers are equal
Bti
/* Write a program to calculate unit and find out electricity bill */
2. main()
{
int units;
float bill;
clrscr();
printf(" enter the units\n");
scanf("%d",&units);
if(units>=200&&units<500)
{
bill=units*2.75;
printf(" the bill is %f\n",bill);
}
else
if(units>=500&&units<=1000)
{
bill=units*5.25;
printf("the bill is %f\n",bill);
}
getch();
}
Bti
Nested if-else :- Syntax :-
If(condition 1)
{
If(condition 2)
{
If(condition 3)
{
statement 1;
}
else
{
statement 2;
}
}
Else
{
statement 3;
}
}
Else
}
}
Else
{
statement 4;
}
Bti
/* program to check whether the year is leap year or not */
Main()
{
Int year;
Clrscr();
Printf(“enter the year”);
Scanf(“%d”, & year”);
If(year %4= = 0)
{
If(year %100 = = 0)
{
If(year %400 = = 0)
{
Printf(“given year is leap year”);
}
else
{
Printf(“given year is not leap year”);
}
}
else
{
Printf(“given year is leap year”);
}
}
Else
{
Printf(“given year is not leap year”);
}
}
Getch();
}
Out put
Enter any year
2004
Given year is leap year
Bti
IF CONDITION WITH OR OPERATOR :-
Syntax :
If(condition1 || condition 2 || condition 3)
{
Block -1 c-statement ;
}
Else
{
Block -2 c-statement;
}
Explatnation Of Syntax :-
In the above syntax if given all condition (or) any one them proves to be true
automatically block-1 of c-statement executes if given all condition proves to be false
the block of 2 c-statement executes.
write a program to read a character than find those whether given character is vowel
or consonant
3. main()
char ch;
clrscr();
scanf("%c",& ch);
else
Bti
printf("given character is consonant");
getch();
Syntax
Switch(variable __name)
Case 1:
c-statement-1;
c-statement-2;
c-statement-n;
break;
case 2:
c-statement-1;
c-statement-2;
c-statement-n;
break;
case 3:
c-statement-1;
c-statement-2;
c-statement-n;
break;
default;
Bti
c-statement;
break;
Explanation of syntax:-In the above syntax switch is a key word followed by switch
variable value. Which is automatically switches depends upon the switch variable
value each and every case closing with break key word
switch(num)
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("THREE");
break;
case 4:
printf("FOUR");
break;
case 5:
printf("FIVE");
break;
Bti
default:
printf("invalid choice");
break;
}
getch();
}
Write a program to read two numbers than calculate add, mul,sub,divi using switch
controlled statemtnt
Main()
{
Int a,b,c;
Int choice;
Clrscr();
Printf(“enter any two values”);
Scanf(“%d%d”,&a,&b);
Printf(“Enter your choice\n”);
Printf(“1.addition”);
Printf(“2.substraction”);
Printf(“3.multiplication”);
Printf(“4.division”);
Scanf(“%d”,&choice);
Switch(choice)
{
Case 1:
C=a+b;
Printf(“the add is =%d”,c);
Break;
Case 2:
C=a-b;
Printf(“the sub is =%d”,c);
Break;
Case 3:
C=a*b;
Printf(“the mult is =%d”,c);
Break;
Case 4:
C=a/b;
Printf(“the divi is =%d”,c);
Break;
Default :
Printf(“ invalid choice”);
Break ;
}
Getch();
}
Bti
Write a program to read alphabet than find out whether given alphabets is vowel or
consonant using switch controlled statement
Main()
{
char character;
clrscr();
printf("enter any alphabets");
scanf("%c",& character);
switch (character)
case ‘a’:
printf("vowel");
break;
case ‘e’:
printf("vowel");
break;
case ‘I’:
printf("vowel");
break;
case ‘o’:
printf("vowel");
break;
case ‘u’:
printf("vowel");
Bti
break;
default :
printf("alphabets is consonant ");
break;
}
getch();
Bti
case 3:
printf("your amount deposited is =%d",ad);
break;
case 4:
printf("your amount withdraw is=%d",aw);
break;
default:
printf("invalid choice");
break;
}
getch();
}
LOOPS:-
Loops are used when a step of statement to be executed repeated for a fixed no. of
times (or) until some condition is true .
Looping is also called as “Repetitive or Iterative”
it is used when the no. of statement is to be executed repeatedly until the specified
condition is true (or) it is used when we don’t know how many times the body of
statement is repeated.
Syntax :-
While(condition)
{
Block c-statement
Increment /decrement
}
/*Write a program to display the given massage 10 time using while loop statement
Main()
{
Int i=1;
Clrscr();
While(i<=10)
{
Printf(“welcome to loops”);
I=i+1;
Bti
}
Getch();
}
Output
Write a program to display the natural numbers in reveres order using reveres orders
main()
{
int i=100;
clrscr();
while(i>=1)
{
printf("%d\t",i);
i=i-1;
}
getch();
}
Write a program to display the natural numbers from 1 to 100 using while loop
statemnt
5. main()
{
long int i=1;
clrscr();
while(i<=100)
{
printf("%d\t",i);
I = I+1;
}
getch();
}
Write a program to read 3 digits numbers than calculate sum of a given digits using
while loop statement
main()
{
int num,sum=0;
clrscr();
printf("enter any value");
scanf("%d",&num);
while(num>0)
{
sum=sum+num%10;
num=num/10;
}
printf("the sum given digits=%d",sum);
getch();
}
Output
I II
Sum= sum + num %10 num=num/10
3=0+3 123/10=12
5=3+2 10/10=1
5+1=6 1/10=0
Enter any value 123
Bti
The sum given digits =6
Output
123
I II
K=k*10+num%10 num=num/10
=0*10+3 12=123/10
K= 0+3
=3*10+2 12/10=1
=30+2
=32
=32*10+1 1/10=0
=320+1
Enter any value 123
num is reverce order =321
Bti
printf("enter numbers");
scanf("%d",&n);
i=0;
while(n>0)
{
n=n/10;i++;
}
printf("lenth =%d\n",i);
getch();
}
Out put
Enter number 1234
Lenth =4
Write a program to read the number then find out whether given numbers is
palindrome numbers or not
main()
{
int num,k=0,j;
clrscr();
printf("enter any value");
scanf("%d",&num);
j=num;
while(num>0)
{
k=k*10+num%10;
num=num/10;
}
printf("reverce order=%d\n",k);
if(j==k)
{
printf("given number is palindrome");
}
else
{
printf("given number is not palindrome");
}
getch();
Bti
}
output
Enter any value 222
Reverse order =222
Given number is palindrome
DO WHILE LOOP :-
Do while it is called post check
do while loop should be ended with ( ; ) .
Do while are keywords.
Expression can be any condition which results in true or false
Syntax
Do
{
Block of c-statement
Increment /decrement
}
While (expression /condition);
Main()
{
Int a=1;
Clrscr();
While(a<50)
{
Printf(“welcome\n”);
A=a+1
}
Getch();
}
Bti
Main()
{
Int a=1;
Clrscr();
do
{
Printf(“welcome\n”);
A=a+1
}While(a>50);
Getch()
Write a program to display the natural no. from 1-100 using do while loops
Main()
{
Int a=100;
Clrscr();
Do
{
Printf( “%d”,&a);
A=a-1
}while(a>=1);
Getch();
}
6. main()
{
int num,i=1,sum=1;
clrscr();
printf("enter any value");
scanf("%d",&num);
while(i<=num)
{
sum=sum*i;
i=i+1;
}
printf("factorial of a given num=%d",sum);
getch();
}
Bti
Write a program to read the number then find out whether given numbers is
palindrome numbers or not
Main()
{
Int num,k=0
Clrscr();
Printf(“enter any values”);
Scanf(“%d”,&num);
Do
{
K=k+num%10;
Num=num/10;
}while(num>0)
Printf(“the number in reverse order =%d”,k);
Getch();
}
It is used when the user knows how many time a set of statement are executed
For is a key word
It can be a single statement or block /compound
Minimum no.of times the execution takes place is zero
{
Block of c-statement
}
Write a program to display the natural numbers from 1-10 using for loops
Main ()
{
Int I;
Clrscr();
For(i=1;i<=100;i++)
{
Printf(“%d\t”,i);
}
Getch();
}
Bti
11111
1111
111
11
1*/
Include<stdio.h>
Main()
{
Int i,j,k;
Clrscr();
For(i=1;i<=5;i++)
{
For(k=0;k<=I;k++)
Printf(“ “);
For(j=5;j>=I; j--)
{
Printf(“1”);
}
Printf(“\n”);
}
}
Bti
*/
#include<stdio.h>
Main()
{
Int i, j;
Clrscr();
For(i=1;i<=5;i++)
{
For(j<=1;j<=i; j++)
{
Printf(“*”);
}
Printf(“\n”);
}
}
/*write a program to print the following output
1
12
123
1234
12345
*/
#include<stdio.h>
Main()
{
Int i, j;
Clrscr();
For(i=1;i<=5;i++)
{
For(j=1;j<=i; j++)
{
Printf(“%d”,j);
}
Printf(“\n”);
}
}
#include<stdio.h>
Main()
{
Int i, j;
Bti
Clrscr();
Printf(“1\n”);
For(i=1;i<=5;i++)
{
For(i=1;j<=i; j++)
{
For(j=1;j<=I;j++)
Printf(“%d”,j);
}
Printf(“1\n”);
}
}
#include<stdio.h>
Main()
{
Int i, j;
Clrscr();
For(i=1;i<=5;i++)
{
For(j=1;j<=I;j++)
{
Printf(“%d”,i);
}
Printf(“\n”);
}
}
main()
{
int n,i,k;
clrscr();
printf("enter numbers");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
k=n*i;
printf("%d*%d=%d\n",n,i,k);
}
Bti
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,k=0;
clrscr();
printf("enter number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
k++;
}
if(k= =2)
printf("the given number is prime %d",n);
else
printf("the given number is not prime %d",n);
getch();
}
BREAK STATEMENT :-
Break;
Figure :
While (condition)
{
-------------------------
-----------------
Break;
-----------------
----------------------
}
Program :-
/* Program to demonstrate break statement.
# include <stdio.h>
#include<conio.h>
Main( )
Bti
{
Int i;
Clrscr();
For (i=1;i<5; i++)
{
If(i>5)
Break;
Printf(“%d”,i); //5 times only
}
Getch();
}
OUT PUT
1234
CONTINUE STATEMENT:
Sometimes, is required to skip a part of a body of loop under specific conditions. So,C
supports ‘continue’ Statement to overcome this anomaly.
The working structure of ‘continue’ is similar as that of that break statement but
difference is that it cannot terminate the loop. It causes the loop to be continued with
next iteration after skipping statements in between. Continue statement simply skipps
statements and continues next iteration.
Syntax :
Continue;
Figure:
While(condition)
{
-------------------
------------------
Continue;
--------------
------------
Program:
#include<stdio.h>
#include<conio.h>
Void main( )
{
Bti
Int i;
Clrscr();
for(i=1;i<=10;i++)
{
If(i= = 6)
Continue;
Printf(“\n\t %d”,i); // 6 is omitted
}
output
1
2
3
4
5
6
7
8
9
10_
Goto Statement :
Syntax :
goto [expr];
figure :
while {condition }
{
For(; ; ; )
{
----------------
-------
Goto err;
-------------------
--------------------
Bti
Err :
}
ARRAYS :-
A Variable can hold a single value but there are many applications which
require to process a group of data items that are of same type .
So we need to declare more variables but it is a complex task
Example:- to get the marks of 50 students we have to declare 50 variables which
is very difficult
To overcome these ‘c’-provides a facility by declaring an array.
By using array we can declare a single variable and store 50 student marks and
can be accessed only by single name
Syntax :-
Data type array –name [size]
Note :-
In any arrays you can store any single of data whether integer or float or character we
cannot store different data types values in single array
Example :-
Main()
{
Int a[3];
Clrscr();
Printf(“Enter any two values”);
Scanf(“%d%d”,&a[0],&a[1]);
a[2]=a[0]+a[0];
printf(“the addition is =%d”,a[2]);
getch();
}
/* Reading and printing an array \*
main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("enter anay no.[%d]",i)
Bti
scanf("%d",&a[i]);
}
printf("the element are:");
for(i=0;i<5;i++)
{
printf("%d\t",a[i]);
}
getch();
}
Output
Enter any no.[0]5
Enter any no.[1]10
Enter any no.[2]15
Enter any no.[3]20
Enter any no.[4]25
The element are : 5 10 15 20 25
7. main()
{
int a[10] ,I ;
clrscr();
printf("enter any 10 values");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("the valus entered are:\n");
for(i=o;i<10;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Write a program to read 10 values in an single array than calculate some of a given
integers 20 values using single dimension array
8. main()
Bti
{
int a[10],i,sum=0;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
sum=sum+a[i];
}
printf("sum of given digits are=%d",sum);
getch();
}
/*write a c program to search an element and its position in one dimensional array */
#include<stdio.h>
#include<conio.h>
main()
{
static int n,a[20],i,fin;
clrscr();
printf("enter who many elements do you want\n");
scanf("%d",&n);
printf("enter elementsinto an array\n");
for(i=0;i<n;i++)
{
printf("\n enter elements:");
scanf("%d",&a[i]);
}
printf("enter the searching element:");
scanf("%d",&fin);
for(i=0;i<n;i++)
{
if(fin==a[i])
{
Bti
printf("element position at location %d",i+1);
break;
}
}
if(i==n)
printf("element not found");
getch();
}
Output
Enter who many elements do you want
10
Enter elements in to an array
Enter elements: 10
Enter elements: 20
Enter elements: 30
Enter elements: 40
Enter elements: 50
Enter the searching elements: 30
Element position at location: 3
multi-dimensional array, in which we index into the array using an ordered list
MEMORY ALLOCATION :-
Row
Bti
/* write a c program multiplication of two matrix */
#include<stdio.h>
#include<conio.h>
main()
{
int k,i,j,p,q,m,n,a[10][10],b[10][10],c[10][10];
clrscr();
printf("enter first matrix row and coloums:");
scanf("%d%d",&m,&n);
printf("enter second matrix row and coloums:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("enter elements into first array:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\n enter elements");
scanf("%d",&a[i][j]);
}
}
printf("enter elements in to second array:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\n enter elements");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",b[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%3d",b[i][j]);
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=a[i][j]*b[k][j];
Bti
}
}
printf("multiplication of martix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%3d",c[i][j]);
printf("\n");
}
}
else
printf("multiplication is not possible");
getch();
}
Output :
Enter first matrix row and columns :
2
2
Enter second matrix row and columns :
2
2
Enter elements into first array :
Enter elements 2
Enter elements 2
Enter elements 2
Enter elements 2
Enter elements into first array:
Enter elements 2
Enter elements 2
Enter elements 2
Enter elements 2
2 2
2 2
2 2
2 2
Multiplication of matrix is :
4 4
4 4
FUNCTIONS IN C :-
The function is a self contained block of statement which performs a coherent task of
a same kind.
C program does not execute the factions directly. It is required to invoke or call that
function. When a function is called in a program the program then program control
goes to the function body. Then, it executes the statements which are involved in a
function body. Therefore, it is possible to call function whenever we want to process
that functions statements.
Bti
There are two type of function
1.user defined function
2.pre defined function (Built in function)
The functions which are created by user for program are known as ‘user defined
function
Syntax :-
Void main()
{
// function prototype
<return _type><function_name>([<argu_list>]);
// function call
<function _name>([arguments>]);
}
//function definition
<return_type><function_neme><([argu_list>]);
{
<function _body>;
}
PROGRAM :-
#include<stdio.h>
#include<conio.h>
Void add( )
{
Int a,b,c;
Clrscr();
Printf(“\n enter any 2 numbers : “);
Scanf(“%d%d”,&a,&b);
C=a+b;
Printf(“\n addition is :%d”,c);
}
Void main( )
{
Void add( );
Bti
Add( );
Getch();
}
Output :
ADVANTAGES :-
It is easy to use
Debugging is more suitable for programs.
It reduces the size of a program.
It is easy to understand the actual logic of a program.
Highly suited in case of large programs.
By using functions in a program, it is possible to construct modular and
structured programs.
Syntax :
// Declaration
Void <function _name>(<data_type><var_nm>);
//calls
<function_name>(<var_num>);
//definition
Void<function_name>(<data_type><var_nm>);
{
<function_body>;
---------------------;
}
Program:
#include<stdio.h>
#include<conio.h>
Void printno(int a)
{
Printf(“\n number is :%d”,a);
}
Void main()
{
Bti
Int no;
Void printno( int);
Clrscr( );
Printf(“\n enter number: “);
Scanf(“%d”,&no);
Pirntno(no);
Getch();
}
Output
Enter number ; 21
Number is : 21
Function call by returning value :-
When a function returns value of variables then that function is known as ‘function
call by returning values’
Syntax :
// declaration
<data_type><function_name>( );
// calls
<variable_of_function>=<function_nm>( );
//definition
<data_type><function_ name> ( )
{
<function_body>;
---------------------
Return<variable_of_function>;
PROGRAM :-
#include<stdio.h>
#include<conio.h>
Int number( )
{
Int no;
Printf(“\n enter number :”);
Scanf(“%d”,&no);
Return no;
}
Void main( )
{
Bti
Int no;
Int number ( );
Clrscr( );
No=number( );
Printf(“\n number is :%d”, no);
Getch( );
}
Output :
Enter nmber : 5
Number is : 5
Program :
#include<stdio.h>
#include<conio.h>
Int number( int n)
{
Return n;
}
Void main( )
{
Int number ( int);
Int a=number(4);
Clrscr( );
Printf(“\n number is : %d”, a);
Getch( );
}
Output :-
Number is : 4
Recursion(Recursive function) :
When a function of body calls the same function then it is called ‘recursive function.
Example :-
Recursion ( )
{
Printtf(“recursion ! “);
Recursion ( );
Bti
}
#include<stdio.h>
#include<conio.h>
Recursion ( )
{
Int no;
Printf(“\n recursion …….”);
Printf(“\n\n enter number : “);
Scanf(“%d”, &no);
If(no= =3)
Exit (0)
Else
Recursion ( );
}
Void main( )
{
Clsrcr( );
Recursion ( );
}
Output
Recursion….
Enter number: 2
Recursion…..
Enter number: 1
Recursion…..
Enter number: 3
Features:
Advantages :
It is easy to use.
It represents compact programming strctures.
Disadvantages :
Bti
It is slower than that of looping statements because each time function is
called.
Note :
Storage Class :
‘Storage’ refers to the scope of a variable an memory allocated by compiler to store
that variable. Scope of a variable is the boundary within which a variable can be used.
Storage class defines the. The scope and lifetime of a variable.
From the point view of C compiler, a variable name identifies physical location from
a computer where variable is stored. There are to memory locations is a computer
system where variables are stored as : Memory and CPU Registers.
Set initial value of a variable or if not specified then setting it do default value.
Syntax
auto [date_type] [variable_name] ;
Bti
Example :
Auto int a ;
Program
/* Program to demonstrate automatic storage class.
#include <stdio.h>
#include<conio.h>
{
auto int i=10;
clrscr( );
{
auto int i=20;
printf(“\n\t %d”,i);
}
Printf(“\n\n\t %d”,i);
Getch( );
}
Output :
20
10
Register Storage Class :
Keyword : register
Storage Location : CPU Register
Initial Value : Garbage
Life : Local to the block in which variable is declared.
Scope : Local to the block
Syntax :
Example :
Register int a;
When the calculation are done in CPU, then the value of variables are transferred
from main memory to CPU. Calculations are done and and the final result is senty
back to main memory. This leads to slowing down of processes.
Register variables occur in CPU and value of that register variable is stored in register
with in that CPU. Thus ,it increases the resultant speed of operations. There is no
waster of time, getting variables from memory and sending it to back again.
Bti
Unary and address of (&) cannot be used with these variables as explicitly or
implicitly
Program :-
#include <stdio.h>
#include<conio.h>
Void main( )
{
Output
20
10
Keyword : static
Storage location : main memory
Initial value : zero and can be initialize once only
Life : depends on function calls and the whole application or program
Scope : local to the block
Syntax :
Example :
Stactic int a;
Bti
Static storage class can be used only if we want the value of a variable to persist
between different function calls.
Program :
#include <stdio.h>
#include<conio.h>
Void main( )
{
Int I;
Void incre ( void );
Clrscr( );
For( i=0;i<3;i++)
Incre( );
Getch( );
}
Void incre( void)
{
Int avar =1;
Static int svar =1;
Avar + +;
Svar + +;
Printf(“\n\n automatic variable value : %d”, avar);
Printf(“\t static variable value : %d”,svar);
Output
Keyword : extern
Storage location : main memory
Initial value : zero
Life : until the program ends.
Scope : Global to the program.
Bti
Syntax :
Example :
Extern int a;
The variable access time is very fast as compared to other storage classes. But few
registers are available for user programs.
The variables of this class can be referred to as ‘global or external variable.’ They
are declared outside the function and can be invoked at anywhere in a program
PROGRAM
#include <stdio.h>
#include<conio.h>
20
10
PRE-DEFINED FUNCTION :-
Bti
4.strrev (string)- this function is used to convert into reverse order
5.strcpy (string1. string2)- this function is used to copy the string from one variable to
another variable
Void main()
{
Chr name[10]
Clrscr();
Printf(“enter any name’);
Scanf(“%s”,name);
Printf(“the length of the given string is =%d\n” ,strlen(name));
Printf(“upper case =%s\n”,strupr(name));
Printf(“lower case=%s\n”,strlwr(name));
Printf(“reverse order=%s\n”,strrev(name));
Getch();
}
Write a program to read string then copy it in another variable using strcpy
Pre-defined.
Void main()
{
Char name1 [10]=”rajeev’,name2 [10]
Clrscr();
Printf(“name1=%s\n”,name1);
Strcpy(name2,name1);
Printf(“name2=%s\n”name2);
Getch();
}
#include<stdio.h>
#include<string.h>
Bti
main()
char string1[5],string2[20];
int i,temp=0;
clrscr();
gets(s1);
gets(s2);
result=strcmp(s1,s2);
if(result ==0)
else
getch();
STRING HANDLING IN C :-
String :-
A string is a collection of characters. Strings are always enclosed in double quotes as
“ string_constant”.
String are used in string handling operations such as,
Counting the length of a string.
Bti
Comparing two strings.
Copying one string to another.
Converting lower case string to upper case.
Converting upper case sring to lower case
Joining two strings.
Reversing string.
Declaration :
The string can be declared as follow :
Syntax :
Char string_nm[size]
Example ;
Char name [50];
String structure :
When compiler assigns string to character array then it automatiocally supplies null
character (‘/0’) at the end of string. Thus, size of string = original length of string +1.
Char name [7];
Name = “TECHNO”
T E C H N O \0
1 2 3 4 5 6 7
Read strings :
Char name[50];
Scanf(“%s”, name);
The above format allows accepting only string only string which does not have any
Write strings ;
To write a string , we can use printf( ) function with format specifier %s.
Scanf(“%s”,name);
Printf(“%s”,name);
Bti
‘String.h’is a header file which includes the declarations, functions, constants of string
handling utilities. These string functions are widely used today by many programmers
to deal with string operations.
/* write programming on
a) to find length of a string
b) to compare two strings
c) to concatenate two strings
d) to copy of two string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[20],str2[20],str3[20];
static int l,k;
clrscr();
printf("enter a string");
gets(str1);
printf("enter aqnother string");
gets(str2);
l=strlen(str1);
printf("length of string is :%d\n",l);
k=strcmp(str1,str2);
if(k==0)
printf("both string are equiol:\n");
else
printf("both string are not equiol:\n");
strcat(str1,str2);
printf("string concatenate two string:%s\n",str1);
strcpy(str3,str1);
printf("copy of two strings:%s\n",str3);
getch();
}
output :
enter a strings: sandeep
enter a other string : harish
length of string is :7
both string are not equal :
string concatenate two string : sandeepharish
copy of two strings :sandeepharish
STRUCTURES :-
Structures are used to store the data in a table form It is also called user
defined data type with the help of Structures we can create a data type through
Bti
which we can store different data type values like Integers, Float and
character in single data type (user defined data type )
Syntax :-
main()
{
struct book
{
int slno;
float bprice;
char bname[10],aname[10];
};
struct book b1,b2;
clrscr();
printf("enter the book details");
scanf("%d%s%s%f",&b1.slno,&b1.bname,&b1.aname,&b1.bprice);
printf("enter book 2 details");
scanf("%d%s%s%f",&b2.slno,&b2.bname,&b2.aname,&b2.bprice);
printf("***************************************************************
*****************\n");
printf("slno\tbname\t\taname\t\tbprice\n");
printf("***************************************************************
*****************\n");
printf("%d\t%s\t\t%s\t\t%f\n",b1.slno,b1.bname,b1.aname,b1.bprice);
printf("%d\t%s\t\t%s\t\t%f\n",b2.slno,b2.bname,b2.aname,b2.bprice);
getch();
}
#include<stdio.h>
Bti
struct student
{
char name[20];
int rollno;
float marks;
};
main()
{
struct student s1={"abc",1,450};
struct student s2;
clrscr();
printf("enter student name,rollno,marks:\n");
scanf("%s%i%f",&s2.name,&s2.rollno,&s2.marks);
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
main()
{
struct student s1={"abc",1,450};
struct student s2={"raju",2,600};
clrscr();
printf("enter student name,rollno,marks:\n");
Bti
#include<stdio.h>
main()
{
struct stud
{
int rno,m,p,c,tot;
char sname[10];
float avg;
};
struct stud s[2];
{
int i,n;
clrscr();
for(i=0;i<2;i++)
{
printf("enter details");
scanf("%d%s%d%d%d",&s[i].rno,&s[i].sname,&s[i].m,&s[i].p,&s[i].c);
s[i].tot=s[i].m+s[i].p+s[i].c;
s[i].avg=s[i].tot/3.0;
}
printf("**********************************\n");
printf("rno\tsname\tmat\tph\tche\ttot\tavg\t\n");
printf("***********************************\n");
{
for(i=0;i<2;i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%f\n",s[i].rno,s[i].sname,s[i].m,s[i].p,s[i].c,
s[i].tot,s[i].avg);
}
getch();
}
}
Bti
cid, cname, producti p.price, discount
main()
{
struct consumer
{
int cid,pprice,discount,tot;
char cname[10],product[10];
};
struct consumer c[2];
{
int i,n;
clrscr();
for(i=0;i<2;i++)
{
printf("enter details\n");
scanf("%d%s%s%d%d",&c[i].cid,&c[i].cname,&c[i].product,&c[i].pprice,&c[i].disco
unt);
c[i].tot=c[i].pprice-c[i].discount;
c[i].discount=c[i].pprice*5/100;
}
printf("**********************************************************\n");
printf("cid\tcname\tproduct\tpprice\tdiscount\ttot\n");
printf("**********************************************************\n");
{
for(i=0;i<2;i++)
printf("%d\t%s\t%s\t%d\t%d\t%d\n",c[i].cid,c[i].cname,c[i].product,c[i].pprice,c[i].di
scount,c[i].tot);
}
getch();
}
}
Bti
Write a program to read employes details with the following field
Eid , ename , edesign e salary.
#include<stdio.h>
main()
{
struct stud
{
int bsal,da,hra,pf,gs;
char ename[10];
float net;
};
struct stud s[2];
{
int i,n;
clrscr();
for(i=0;i<2;i++)
{
printf("enter details\n");
scanf("%s%d%d%d%d",&s[i].ename,&s[i].bsal,&s[i].da,&s[i].hra,&s[i].pf);
s[i].gs=s[i].bsal+s[i].da+s[i].hra+s[i].pf;
s[i].net=s[i].gs-s[i].pf;
}
printf("***********************************************\n");
printf("ename\tbasicsal\tda\thra\tpf\tgs\tnet salary\n");
printf("************************************************\n");
{
for(i=0;i<2;i++)
printf("%s\t%d\t\t%d\t%d\t%d\t%d\t%f\n",s[i].ename,s[i].bsal,s[i].da,s[i].hra,s
[i].pf,s[i].gs,s[i].net);
}
getch();
}}
Bti
POINTER :-
When variables are declared memory is allocated to each variable and these variables
are used to store some information or data .pointer is a special variable which holds
the address of another variable or identifier pointer allow indirect access of data.
Pointer is a variable which is use to store the address of normal variables
Declaration :- to differentiate ordinary variable from pointer variable, the pointer
variable should be preceded by value at address operator [*]
syntax :-
datatype *variable
example *ptr;
write a program to read 2 numbers and exces a,b, values & address store ‘a’ addres in
main()
int a,b;
int *ptr;
clrscr();
scanf("%d%d",&a,&b);
ptr=&a;
getch();
Bti
The value of variable can be accessed in two way (a)by using ordinay variable (b) by
using pointer variable or call by value
Ex:-
main()
{
int a=25,*x,*y,*z;
clrscr();
x=y=z=&a;
printf("%d\n",*x);
printf("%d\n",*y);
printf("%d\n",*z);
printf("%d\n",a);
printf("%d\n",*(&a));
getch();
}
Output
25
25
25
25
25
Output
100
2000
Disadvantages of pointers:-
Unless defined and initialized properly used of pointer may lead to disastrous
situation .
Sometime using pointer is confusing them ordinary methods
Bti
Void pointer :-
We can declare a pointer to be of void data type and can assign a void pointer to any
other type
main()
int x=100;
void=*p;
p=&x;
printf("%d",*p);
It gives an error because pointer ‘p’ is of void data type and it cannot hold any value.
main()
int x=100;
void=*p;
p=&x;
printf("%d",*(int*)p);
Out put
100
The void pointer can be type casted to any other type during execution of the
program.
Bti
POINTER AND FUNCTION ;-
In call by reference what ever changes are made to formal arguments of the
function definition will effect the actual arguments in the calling function.
address
Bti
POINTER AND ARRAYS
display(int *x)
main()
{
int i,n,a[10];
clrscr();
printf("enter the size of an arrray");
scanf("%d",&n);
printf("enter the array element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the array element are:");
for(i=0;i=n;i++)
{
display(&a[i]);
}
}
display(int *x)
{
printf("%d\t",*p);
}
Output
Enter the size of an array
5
Enter the array element
1
2
3
4
5
Bti
The array elements are :1 2 3 4 5
HEADER FILE IN C :-
Header file contains different predefined functions, which are required to run the
program all heard file should be include explicitly before main( ) function.
Method 1 is used to link header files in current directory as well as specified dirctories
using specific path. The path must be upto 127 characters. This is limit of path
declaration. Method 2 is used to link header files in specified dirctories only.
Followings are the some commonly used header files which plays a vital role in C
Programming.
Stdio.h
Conio.h
Math.h
Graphics.h
Bti
Math.h header file :
Math.h is a header file which is commonly used for mathematical operations. Some
functions of this header file uses floating point numbers. The functions which accepts
angle are accepted in terms of radians.
Programme :
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
printf("\n\t log of 10 :%f",log(10));
printf("\n\n\t squar root of 8:%f",sqrt(8));
printf("\n\n\t square of 4 :%f",pow(4,2));
printf("\n\n\t sine of 10 :%f",sin(10));
getch();
}
Output
Log of 10 : 2.302585
Square of 4 : 16.000000
Bti
Sine of 10 : -0.544021
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
clrscr();
initgraph(&gdriver,&gmode,"c:\\tt\\bgi");
circle(70,70,20);
getch();
closegraph();
}
Output
Bti