C Language
C Language
PROGRAMMING IN
C LANGUAGE
CONTENT
Chapter 1 INTRODUCTION TO C LANGUAGE
1.1 HISTORY OF C LANGUAGE
1.3 VARAIBLE
1.5 SPECIFIERS
Chapter 2 OPERATORS
2.1 ARITHMETIC OPERATOR
3.2 IF STATEMENT
Chapter 4 LOOPS
4.1 INTRODUCTION
3 C LANGUAGE
4.6 NESTING
Chapter 5 ARRAY
5.1 1-D ARRAY
5.2 2D ARRAYS
5.3 POINTER
Chapter 6 FUNCTION
6.1 INTRODUCTION
6.2 RECURSION
6.4 STRING
7.2 UNION
C language Timeline
Programming Development
Developed by
Language Year
International
ALGOL 1960
Group
BCPL 1967 Martin Richards
B 1970 Ken Thompson
C 1972 Dennis Ritchie
void void 0 NA
Example:-
int a;
int a123;
int abc;
int _123;
int _;
int 123a;(wrong)
getch();
}
Output:-
Well-come to c language
Example2:-A.c
void main()
{
printf(“well-come to c language”);
printf(“Thanks for visit”);
getch();
}
Output:-
Well-come to c languageThanks for visit
Example3:-A.c
void main()
{
printf(“well-come to c language\n”);
printf(“Thanks for visit”);
getch();
}
11 C LANGUAGE
Output:-
Well-come to c language
Thanks for visit
Example4:-A.c
void main()
{
printf(“well-come to c language”);
printf(“\nThanks for visit”);
getch();
}
Output:-
Well-come to c language
Thanks for visit
Example5:-A.c
void main()
{
printf(“well-come to c language\t”);
printf(“Thanks for visit”);
getch();
12 C LANGUAGE
Output:-
Output:-
Example8:-A.c
void main()
{
printf(“Well-come to c language\nThanks for visit”);
getch();
}
Output:-
Well-come to c language
Thanks for visit
1.5 SPECIFIERS
Conversion specifier for different data types
14 C LANGUAGE
Example9:-A.c
void main() {
int a,b;
printf(“%d”,a);
printf(“%d”,b);
getch();
}
Output:-
Garbadge value
Garbadge value
Example10:-A.c
void main() {
int a,b;
a=100;
b=200;
15 C LANGUAGE
printf(“%d\n”,a);
printf(“%d”,b);
getch(); }
Output:-
100
200
Example11:-A.c
void main()
{
int a,b;
a=100;
b=200;
printf(“a=%d\n”,a);
printf(“b=%d”,b);
getch();
}
Output:-
a=100
b=200
Example12:-A.c
void main()
16 C LANGUAGE
{
int a,b;
a=100;
b=200;
printf(“Value of a=%d\n”,a);
printf(“Value of b=%d”,b);
getch();
}
Output:-
Value of a=100
Value of b=200
Example13:-A.c
void main()
{
int a=100,b=200;
printf(“Value of a=%d\n Value of b=%d”,a,b);
getch();
}
Output:-
Value of a=100
Value of b=200
17 C LANGUAGE
Example14:-A.c
void main()
{
int a,b;
printf(“Enter any two numbers\n”);
scanf(“%d”,&a);
scanf(“%d”,&b);
printf(“\nValue of a=%d\n Value of b=%d”,a,b);
getch();
}
Output:-
Enter any two numbers
100
200
Value of a=100
18 C LANGUAGE
Value of b=200
Chapter 2 OPERATORS
Operators:-If we want to perform some business related operations, we
need operators. Operator need variable for operate.
19 C LANGUAGE
Types of operators:-
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Bitwise operator
5. Sizeof operator
6. Comma operator
7. Assignment operator
8. Dot operator
Example: - +,-,*, /, %.
void main() {
int a,b,c;
printf("Enter two no\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d\n",c);
c=a-b;
printf("sub=%d\n",c);
c=a*b;
printf("mul=%d\n",c);
c=a/b;
printf("div=%d\n",c);
c=a%b;
printf("mod=%d\n",c);
getch();
}
Example2:- program for binary arithmetic operation.
void main()
{
int a,b;
printf("Enter two no\n");
scanf("%d%d",&a,&b);
printf("sum=%d\n",a+b);
printf("sub=%d\n",a-b);
printf("mul=%d\n",a*b);
printf("div=%d\n",a/b);
printf("mod=%d\n",a%b);
getch(); }
21 C LANGUAGE
Example:-
Case2:-
int a=-15;
if input value is negative than inserted bits will be 1.
Case2:-
int a=-15;
void main()
{
char c;
int i;
long l;
float f;
double d;
printf("size of charactor is %d Byte\n",sizeof(c));
printf("size of integer is %d Byte\n",sizeof(i));
printf("size of long is %d Byte\n",sizeof(l));
printf("size of float is %d Byte\n",sizeof(f));
printf("size of double is %d Byte\n",sizeof(d));
getch();
}
Example3:- program for finding size of data type.
void main()
{
char c;
int i,size;
long l;
float f;
double d;
size=sizeof(c);
printf("size of charactor is %d Byte\n",size);
30 C LANGUAGE
size=sizeof(i);
printf("size of integer is %d Byte\n",size);
size=sizeof(l);
printf("size of long is %d Byte\n",size);
size=sizeof(f);
printf("size of float is %d Byte\n",size);
size=sizeof(d);
printf("size of double is %d Byte\n",size);
getch();
}
a=10;
b=a;
c=b;
}
3.1 INTRODUCTION
Control statements are used to control flow of execution or control
statements are used to take some decisions, like validations and security.
Control statements play very important roll where something should be
happened before satisfying some constraints or conditions, after that
result should be generated according to the constraints satisfaction or
dissatisfaction. Without using control statements nobody can develop a
scalable software or application. Control statements are much important
in business logic development.
For example someone want to withdraw money from his/her account so
in this process before withdraw money first check the available balance
must be greater or at least equal to the requested amount then only
money should be withdraw otherwise provide some insufficient balance
type messages.Another example is examination result generating system
in this process marks and some threshold values must be compare and
then produce results according to the applied conditions.
Types of control statements
1. if statement
2. if-else statement
3. switch-case statement
4. break statement
5. continue statement
6. goto-label statement
7. return statement
3.2 IF STATEMENT
33 C LANGUAGE
if(condition) if(condition)
Statement1; or {
Statement1;
}
In the above diagram if the given condition is true then only statement1
will be execute. If the given condition is false then statement1 will not
execute. We can write above if statement like
If we are not using curly brasses then only just first statement will be
inside if statement only.
If(condition) If(condition)
Statement1; {
Statement2; or Statement1;
Statement3; }
Statement2;
Statement3;
If(condition)
{
Statement1;
Statement2;
Statement3;
}
In the above if statement all three statements are inside if, all three will
be execute when condition will true otherwise all three statements will
not execute.
Example1:-program for result generation.
#include<stdio.h>
#include<conio.h>
void main()
{
int sub;
printf(“Enter your marks for a subject”);
scanf(“%d”,&sub);
if(sub>35)
printf(“you are pass in this sub”);
getch(); }
Run1:-
Enter your marks for a subject
40
You are pass in this sub
35 C LANGUAGE
Run2:-
Enter your marks for a subject
30
In this case our program not producing any message, to solve above
problem we have to check subject marks two times, it is a another
problem to verify same number twice it increase complexity of our
program ,program takes more time for execution.
Example2:-program for result generation.
#include<stdio.h>
#include<conio.h>
void main()
{
int sub;
printf(“Enter your marks for a subject”);
scanf(“%d”,&sub);
if(sub>35)
printf(“you are pass in this sub”);
if(sub<35)
printf(“you are fail in this sub”);
getch();
}
36 C LANGUAGE
Run1:-
Enter your marks for a subject
40
You are pass in this sub
Run2:-
Enter your marks for a subject
30
You are fail in this sub
3.3 IF-ELSE STATEMENT
Problem in if statement is solved by the if-else statement.
Syntax of if –else is:-
If(condition)
Statement1;
else
Statement2;
Or
If(condition){
Statement1;
}
else{
Statement2;
37 C LANGUAGE
}
Above syntax is best suitable for controlling single statement at a time.
In this syntax if the given condition is true then if block will execute and
else block will not execute. If the given condition is not true (false) then
if block will not execute and else block will execute.
If we want to control multiple statements then we have to use curly
brasses.
if(condition){
Statements;
}
else{
Statements;
}
Example1:-greater number between two number.
void main()
{
int a,b;
printf(“enter any two number”);
scanf(“%d%d”,&a,&b);
if(a>b){
printf(“\n greater number is %d”,a);
}
38 C LANGUAGE
else{
printf(“\n greater number is %d”,b);
}
getch();
}
Example2:-program for check the given number is negative or
positive.
void main()
{
int n;
printf(“enter a number”);
scanf(“%d”,&n);
if(n>0)
{
printf(“number %d is positive”,n);
}
else
{
printf(“number %d is negative”,n);
}
getch();
39 C LANGUAGE
}
Example3:-program for withdraw amount.
void main()
{
int bal=1000;
int w;
printf("Enter requested amont\n");
scanf("%d",&w);
if(bal>=w)
{
printf("\nplease take your amount\withdrawal success");
bal=bal-w;
printf("\nAvailable amount is %d",bal);
}
else
{
printf("\n Insufficient fund");
}
getch();
}
3.4 SWICH-CASE STATEMENT
40 C LANGUAGE
In if-else statement we have two options if block and else block but at a
time only one option executed either if either else. We user want to
execute multiple options then we have to go for switch-case statements.
Here both switch and case are keyword.
Syntax of switch-case:-
switch (n)
{
case 1:statement1;
case 2:statement2;
case 3:statement3;
default: default statement;
}
or
switch (n)
{
case 1:
{statements;}
case 2:
{statements;}
case 3:
{statements;}
default:
41 C LANGUAGE
{default statements;}
}
Or
switch (n)
{
case 2:statement2;
case 1:statement1;
default: default statement;
case 3:statement3;
}
Or
switch (n)
{
case 1:statement1;
case 2:statement2;
case 3:statement3;
}
Or
switch (n)
{
case „a‟:statement1;
42 C LANGUAGE
case „2‟:statement2;
case „G‟:statement3;
}
In above all syntaxes n may be char or int type and there is no rule for
order of cases, cases may be occur in any order.
How switch-case work it depends on value of n it matches n with cases
from top to down one by one, if any case match then execution start
from that case to below all cases.
Example1:-Demo of switch case.
void main()
{
int n;
printf(“Enter your choice”);
scanf(“%d”,&n);
switch(n) {
case 1:printf(“This is case 1”);
case 2:printf(“This is case 2”);
case 3:printf(“This is case 3”);
default:printf(“This is default case”);
}
getch();}
43 C LANGUAGE
In this syntax statement1 and 2 will normally execute but when control
reached at goto p then control automatically transfer to p label and
statement3 will not execute and statement4 will normally executed.
We can write goto statement in another way like.
Statement1;
Statement2;
p:
Statement3;
goto p;
Statement4;
In the above format statement3 will executed infinite time.
return statement:-return statement is mainly used for function, we will
explain in user define function.
Chapter 4 LOOPS
47 C LANGUAGE
4.1 INTRODUCTION
Basic purpose of loops or iterative statement is to reduce length of
application or code and time of developer.
Types of loop:-
1. for loop
2. while loop
3. do-while loop
4.2 FOR LOOP
Syntax of for loop:-
for(initialization;condition;counter)
statement1;
OR
for(initialization;condition;counter)
{
statement1;
}
48 C LANGUAGE
for(i=1;i<=10;i++)
printf(“%d\n”,i);
for(i=1;i<11;i++)
printf(“%d\n”,i);
for(i=1;i<=10;++i)
printf(“%d\n”,i);
49 C LANGUAGE
for(i=1;i<11;++i)
printf(“%d\n”,i);
for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
}
i=1;
for( ;i<=10;i++)
printf(“%d\n”,i);
for(i=1;i<=10; )
printf(“%d\n”,i++);
i=1 ;
for(;i<=10;)
{
printf(“%d\n”,i++);
}
i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
i++;
}
50 C LANGUAGE
i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
++i;
}
void main(){
int i;
for(i=1;i<=10;i++)
printf(“%d\n”,i);
getch();
}
void main(){
int i;
for(i=1;i<11;i++)
printf(“%d\n”,i);
getch();
}
51 C LANGUAGE
void main(){
int i;
for(i=1;i<=10;++i)
printf(“%d\n”,i);
getch();
}
void main(){
int i;
for(i=1;i<11;++i)
printf(“%d\n”,i);
getch();
}
void main(){
int i;
for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
}
getch();
}
void main(){
int i;
i=1;
for( ;i<=10;i++)
printf(“%d\n”,i);
getch();
}
52 C LANGUAGE
void main(){
int i;
for(i=1;i<=10; )
printf(“%d\n”,i++);
getch();
}
void main(){
int i;
53 C LANGUAGE
i=1 ;
for(;i<=10;)
{
printf(“%d\n”,i++);
}
getch();
}
54 C LANGUAGE
void main(){
int i;
i=1;
for(;i<=10;)
{
printf(“%d\n”,i);
i++;
}
getch();
}
void main(){
int i;
for(;i<=10;)
{
printf(“%d\n”,i);
++i;
}
getch();
}
printf("%c\t",i);
s++;
}
for(i=-1;i>=-128;i--)
{
printf("%c\t",i);
s++;
}
printf("\nC LANGUAGE SUPPORT TOTAL %d CHARACTERS",s);
getch();
}
Example2:-Program for finding ASCII value associated with
character.
#include<dos.h>
void main()
{
int i;
clrscr();
for(i=0;i<=127;i++)
{
printf("character %c Ascii value %d\t",i,i);
56 C LANGUAGE
delay(1000);
}
for(i=-1;i>=-128;i--)
{
printf("character %c Ascii value %d\t",i,i);
delay(1000);
}
getch();
}
4.3 WHILE LOOP
While loop work same as for loop both for and while are entry
controlled loop that means loop will execute if given condition satisfied.
While is a keyword followed by parentheses, inside parenthesis we have
to write valid condition.
Syntax of while loop:-
Initialization;
While(condition)
{
Statements;
}
In the above syntax first variable will initialize once then control goes to
check the given condition is true or not. If the given condition is true
then control goes to inside loop and execute statement then again control
57 C LANGUAGE
transfer for checking condition whenever condition is true then loop will
be executed, once condition will be false then loop will stop.
Example1:-Demo of while loop for print ten times Well-Come.
void main()
{
int i;
printf(“This is demo of while loop\n”);
i=1;
while(i<=10)
{
printf(“Well-Come”);
}
getch();
}
Example2:-print 1 to 10.
void main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
58 C LANGUAGE
i++;
}
getch();
}
Example3:-print 1 to 10.
void main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
++i;
}
getch();
}
Example4:-print 1 to 10.
void main()
{
int i=1;
while(i<=10){
printf(“%d\n”,i++);
59 C LANGUAGE
}
getch();}
4.4 DO-WHILE LOOP
do-while loop is an exit control loop, in this loop statements or body of
loop executed before satisfying conditions for first time that means do-
while loop executed at least once even condition is false. All three loops
are used for same purpose.
Syntax of do-while loop:-
Initialization;
do{
Statements;
}while(condition);
i++;
}while(i<=5);
getch();}
Example2:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i);
i++;
}while(i<=10);
getch();
}
Example3:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i);
61 C LANGUAGE
++i;
}while(i<=10);
getch();}
Example4:-print 1 to 10.
void main()
{
int i=1;
do
{
printf(“%d\n”,i++);
}while(i<=10);
getch();
}
int i;
for(i=1;i<=10;i++)
{
if(i==6)
{
break;
}
printf(“%d\n”,i);
}
getch();
}
Example2:-Demo of break statement.
void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==6)
{
continue;
}
63 C LANGUAGE
printf(“%d\n”,i);
}
getch();
}
4.6 NESTING
Nesting is a control switching process in which some thing happening
under something.
Types of nesting:-nesting is categorized into two categories.
1. Statement nesting
2. Loop nesting
Statement nesting:-in statement nesting, statement run under
another statement, both statement may be same or may be
different.
Example1:-
if(condition) ----------outer statement
{
if(condition)-----------inner statement
{
}
}
Example2:-
if(condition)------------outer statement
{
switch(exp)-------------inner statement
{
case 1:
case 2:
64 C LANGUAGE
………
}
}
Nesting of loop:-loop inside loop called nesting (any loop inside
any loop)
printf(“\n”);
}
getch();
}
}
printf(“\n”);
}
getch();
}
Chapter 5 ARRAY
array is a special type of variable which can store more than one
value into a single variable at a time but all the values must be
similar. Array elements stored sequentially in memory. Array is
also called indexed variable, array index start from 0.
That means whole memory assign to array variable is divide into
array indexed.
a=300
a=300
Example2:-Why need array.
void main()
{
int a;
a=100;
printf(“a=%d\n”,a);
a=200;
printf(“a=%d\n”,a);
a=300;
printf(“a=%d\n”,a);
getch();
}
Output:-
a=100
a=200
a=300
Initialization of array:-
int arr[5]={10,20,30,40,50};
Or
int arr[5]={10,20,30,40};
Or
int arr[]={10,20,30,40,50,60,70};
Or
int arr[5]={};
69 C LANGUAGE
Example1:-Demo of array.
void main()
{
int arr[5]={10,20,30,40,50},i;
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
getch();
}
Example2:-Demo of array.
void main()
{
int arr[5]={50,40,30,20,10},i;
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
getch();
}
Example3:-Demo of array.
void main()
{
int arr[5]={10,20,30,40,50},i;
printf(“array elements are:-\n”);
for(i=4;i>=0;i--)
{
printf(“%d\n”,arr[i]);
70 C LANGUAGE
}
getch();
}
Example4:-Demo of array(input from keyboard).
void main()
{
int arr[5],i;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
scanf(“%d”,&arr[i]);
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
printf(“%d\n”,arr[i]);
getch();
}
Example5:-Demo of array(input from keyboard).
void main()
{
int arr[5],i;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“array elements are:-\n”);
for(i=0;i<=4;i++)
{
printf(“%d\n”,arr[i]);
}
71 C LANGUAGE
getch();
}
Example6:-program for sum of all array elements.
void main()
{
int arr[5],i,s=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}
for(i=0;i<=4;i++)
{
if(arr[i]%2==0)
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}
Example8:-program for sum of all odd elements of array.
void main()
{
int arr[5],i,s=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
if(arr[i]%2!=0)
s=s+arr[i];
}
printf(“Sum of array elements=%d”,s);
getch();
}
Example9:-program for sum of all even and all odd elements of
array.
void main()
{
73 C LANGUAGE
int arr[5],i,even=0,odd=0;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Addition is going on:::::-\n”);
for(i=0;i<=4;i++)
{
if(arr[i]%2==0)
{
even=even+arr[i];
}
else
{
odd=odd+arr[i];
}
}
printf(“\nSum of even elements=%d”,even);
printf(“\n Sum of odd elements=%d”,odd);
getch();
}
Example10:-program for search an element into array.
void main()
{
int arr[5],i,s=0,n;
printf(“Enetr array elements\n”);
for(i=0;i<=4;i++)
{
scanf(“%d”,&arr[i]);
74 C LANGUAGE
}
printf(“\n Enter search element\n”);
scanf(“%d”,&n);
printf(“Searching is going on:::::-\n”);
for(i=0;i<=4;i++){
if(arr[i]==n)
{
s++;
break;
}
}
if(s==0)
printf(“\n element not found\n”);
else
printf(“\n element found at position %d”,i);
getch();}
Example11:-program for check duplicate data occurrence.
void main()
{
int a[5],n,i,s=0;
printf("Enter array Elements\n");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
printf("Enter a search element\n");
scanf("%d",&n);
printf("Searching started...........\n");
for(i=0;i<=4;i++)
{
75 C LANGUAGE
if(a[i]==n)
{
s++;
}
}
if(s==0)
printf("Element not found");
else
printf("Element found at %d times",s);
getch();
}
printf("%d\n",a[i]);
}
printf("Revrsing process start....\nArray elements are ::::-\n");
j=4;
for(i=0;i<size/2;i++)
{
p=a[i];
a[i]=a[j];
a[j]=p;
j--;
}
for(i=0;i<=size-1;i++)
{
printf("%d\n",a[i]);
}
getch();}
5.2 2D ARRAYS
Syntax of declaration:-
Datatype arrayname[size][size];
Here first subscript represents number of row and second subscript
represents number of columns.
For example:-
int arr[2][2]; here arr contains four values that will
77 C LANGUAGE
arr[0][0] arr[0][1]
arr[1][0] arr[1][1]
Here arr[0][0],means first element(zero row ,zero column)
arr[0][1],means second element(zero row ,one column)
arr[1][0],means third element(one row ,zero column)
arr[1][1],means fourth element(one row ,one column)
Array initialization:-
int[2][2]={{11,22},{33,44}};
Or
int[2][2]={11,22,33,44};
int[][2]={11,22,33,44};
}
printf("\n");
}
getch();
}
Example2:- program for matrix hard coded value.
void main(){
int arr[2][2]={11,22,33,44},i,j;
clrscr();
printf("matrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();}
Example3:-program for matrix hard coded value.
void main()
{
int arr[][2]={11,22,33,44},i,j;
clrscr();
printf("matrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
79 C LANGUAGE
}
printf("\n");
}
getch();
}
Example4:-program for matrix input from keyboard.
void main()
{
int arr[2][2],i,j;
clrscr();
printf("Enter matrix elements:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nmatrix is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
Example5:-program for addition of two matrix
80 C LANGUAGE
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
81 C LANGUAGE
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix addition is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
for(i=0;i<=1;i++)
83 C LANGUAGE
{
for(j=0;j<=1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix addition is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Example6:-program for matrix multiplication.
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,k,s=0;
84 C LANGUAGE
clrscr();
printf("Enter elements of matrix A:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nmatrix A is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter elements for matrix B:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nmatrix B is:::::::\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
85 C LANGUAGE
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n matix multiplication is processing...........\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
for(k=0;k<=1;k++)
{
s=s+a[i][k]*b[k][j];
}
c[i][j]=s;
}
s=0;
}
printf("\n matix C is\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
5.3 POINTER
86 C LANGUAGE
Example1:-
void main()
{
int a=10,b;
b=a;
printf(“original value of a=%d\n”,a);
printf(“mirror value of a=%d”,b);
getch();
}
87 C LANGUAGE
Example2:-
void main()
{
int a=10,b;
b=&a; //Error
getch();
}
Example3:-
void main()
{
int a=10,*b;
b=&a;
printf(“original value of a=%d\n”,a);
printf(“original value of a=%d\n”,*b);
printf(“original value of a=%d\n”,*(&a));
printf(“address of a=%u”,&a);
printf(“\n address of a=%u”,p);
getch();
}
acc=acc-w;
}
printf(“\n after withdra account=%d”,acc);
getch();
}
Chapter 6 FUNCTION
6.1 INTRODUCTION
function is a set of instructions which perform some unique task.
Function is a self contained block of statements, which perform
some task.
int sum(int,int);
Function call:-
Functionname(variable,variable,……);
For example:-
sum(a,b);
Example1:-
void fun1();
void fun2();
void main()
{
clrscr();
printf("This is main function");
fun1();
printf("\nControl come back in main function\n");
fun2();
printf("Control come back in main function\n");
92 C LANGUAGE
getch();
}
void fun1()
{
printf("\n This is function1");
}
void fun2()
{
printf("\n This is function2");
}
Example2:-
void fun1();
void fun2();
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
93 C LANGUAGE
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun2();
fun1();
printf("Control come back in main function\n");
fun2();
fun1();
printf("Control come back in main function\n");
getch();}
Example5:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
fun1();
printf("Control come back in main function\n");
95 C LANGUAGE
fun1();
printf("Control come back in main function\n");
getch();
}
Example6:-
void fun1()
{
printf(" This is function1\n");
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun2();
fun2();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example7:-
void fun2();
void fun1()
{
fun2();
printf(" This is function1\n");
96 C LANGUAGE
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();}
Example8:-
void fun2();
void fun1()
{
fun2();
printf(" This is function1\n");
fun2();
}
void fun2()
{
printf(" This is function2\n");
}
void main()
{
clrscr();
printf("This is main function\n");
97 C LANGUAGE
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example9:-
void fun2(void);
void fun1(void)
{
fun2();
printf(" This is function1\n");
fun2();
}
void fun2(void)
{
printf(" This is function2\n");
}
void main(void)
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in main function\n");
fun2();
printf("Control come back in main function\n");
getch();
}
Example10:-
void fun2();
98 C LANGUAGE
void fun3();
void fun1()
{
fun2();
printf(" This is function1\n");
}
void fun2()
{
fun3();
printf(" This is function2\n");
}
void fun3()
{
printf(" This is function3\n");
}
void main()
{
clrscr();
printf("This is main function\n");
fun1();
printf("Control come back in end of main function\n");
getch();
}
Example11:-
void fun2();
void fun3();
void fun1()
{
printf(" This is function1\n");
99 C LANGUAGE
}
void fun2()
{
printf(" This is function2\n");
fun1();
}
void fun3()
{
printf(" This is function3\n");
fun2();
}
void main()
{
clrscr();
printf("This is main function\n");
fun3();
printf("Control come back in end of main function\n");
getch();
}
6.2 RECURSION
Recursion is a process by which function call itself again and again
infinitely, recursion must required a terminating condition for
avoiding infinite calls.
Example1:-
int rec(int);
void main()
{
int n,f;
100 C LANGUAGE
int rec(int n)
{
count++;
if(n==1)
{
printf("function call itself %d time\n",count);
printf("Terminating condition Hit\n");
return 1;
}
else
{
printf("function call itself %d time\n",count);
return(n*rec(n-1));
}
}
Example3:-
int count;
void rec();
void main()
{
int n;
clrscr();
printf("\Function call start\n");
rec();
getch();
}
void rec()
{
count++;
102 C LANGUAGE
if(count==10)
{
printf("function call itself %d time\n",count);
printf("Terminating condition Hit\n");
}
else
{
printf("function call itself %d time\n",count);
rec();
}
}
}
Example2:-program for global variable.
int a=100;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
getch();
}
Example3:-program for global variable.
int a;
void main()
{
int b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("Global variable a=%d\n",a);
printf("Local variable b=%d\n",b);
printf("Sum of Global and Local=%d",a+b);
getch();
}
Example4:-program for global variable.
int a;
void main()
{
int a;
104 C LANGUAGE
disp();
getch();
}
void disp()
{
printf("outside main global variable a=%d\n",a);
printf("outside main(declared in main) local variable=%d\n",b);
}
Output:-Compiling GL6.C:
Error GL6.C 17: Undefined symbol 'b'
void disp()
{
printf("outside main global variable a=%d\n",a);
}
6.4 STRING
107 C LANGUAGE
Example1:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
scanf("%s",name);
printf("\n Well-come %s",name);
getch();
}
Example2:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
getch();
}
Example3:-
#include<string.h>
void main()
{
108 C LANGUAGE
char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
printf("\n Number of character in your name are:-
%d",strlen(name));
getch();
}
Example4:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
gets(name);
printf("\n Well-come %s",name);
printf("\n After passing from strupr:-%s",strupr(name));
getch();
}
Example5:-
#include<string.h>
void main()
{
char name[50];
printf("Enter your name");
109 C LANGUAGE
gets(name);
printf("\n Well-come %s",name);
printf("\n After passing from strupr:-%s",strupr(name));
printf("\n After passing from strrev:-%s",strrev(name));
printf("\n After passing from strlpr:-%s",strlwr(name));
printf("\n After passing from strrev:-%s",strrev(name));
getch();
}
Example6:-
#include<string.h>
void main(){
char n[50],m[50];
printf("Enter your name in first array\n");
gets(n);
printf("Enter your surname in second array\n");
gets(m);
printf("\n Well-come name is %s\t surname is %s\n",n,m);
strcpy(n,m);
printf("\n After passing from strcpy: name is %s\t surname is
%s\n",n,m);
strcat(n,m);
printf("\n After passing from strcat: name is %s\t surname is
%s",n,m);
getch();}
Example7:-
#include<string.h>
void main()
{
char n[50],m[50];
int l;
110 C LANGUAGE
7.1 INTRODUCTION
Variable declaration:-
struct structure name
{
Memeber1;
Member2;
.
.
.
Member n;
}variables;
Example:-
struct book
{
char name[50];
int pages;
float price;
}b;
Initialization:-
struct book
{
char name[50];
int pages;
float price;
};
struct book b={“C language”,200,250.0};
{
struct book
{
char name[50];
int pages;
float price;
};
struct book b={"C Language",200,250.0};
printf("Name of book is %s\n",b.name);
printf("pages=%d\n price=%f",b.pages,b.price);
getch();
}
7.2 UNION
float price;
}s;
union book1
{
char name[50];
int pages;
float price;
}u;
printf("Memory taken by structure %d bytes\n",sizeof(s));
printf("Memory taken by union %d bytes",sizeof(u));
getch();
}
FILE <STDIO.H>
typedef struct {
short level;
unsigned flags;
char fd;
unsigned char hold;
short bsize;
unsigned char *buffer, *curp;
unsigned istemp;
short token;
117 C LANGUAGE
} FILE;
Here FILE is declared as a structure type variable in <stdio.h>
header file. Using the keyword typedef we can create our own type
of variable. Like we can create now FILE type variables, FILE
*fp.
In file handling we can create new file, update file, delete file and
many operation can perform on file.
For performing any type of operations file must be open first, for
opening a file we have to use library function fopen().
Syntax of fopen function:-
fopen(“filename”,”filemode”);
{
printf("file exist");
}
getch();
}
Example3:-program in append mod.
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("a123.c","a");
if(fp==NULL)
{
printf("file not exist\n");
}
else
{
printf("file exist");
}
getch();
}
Example4:-program for writing data into file.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50];
fp=fopen("a123.c","w");
printf("Enter your name\n");
gets(name);
120 C LANGUAGE
fputs(name,fp);
printf("Data inserted successfully");
getch();
}
Example5:-program for reading from a file and writing into
file.
#include<stdio.h>
void main()
{
FILE *fp,*fw;
char name[50],ch;
fp=fopen("a123.c","r");
fw=fopen("a12.c","w");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
fputc(ch,fw);
ch=fgetc(fp);
}
printf("Data inserted successfully");
getch();
}
Example6:-program for counting number of lines.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50],ch;
int s=0;
121 C LANGUAGE
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==‟;‟)
{
s++;
}
ch=fgetc(fp);
}
printf("Total lines %d",s-1);
getch();
}
}
ch=fgetc(fp);
}
printf("Total spaces %d",s-1);
getch();
}
Example8:-program for counting particular character.
#include<stdio.h>
void main()
{
FILE *fp;
char name[50],ch;
int s=0;
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==‟Z ‟)
{
s++;
}
ch=fgetc(fp);
}
printf("Total character %d",s-1);
getch();
}
Example9:-program for counting total character into file.
#include<stdio.h>
void main()
123 C LANGUAGE
{
FILE *fp;
char name[50],ch;
int s=0;
fp=fopen("a123.c","r");
printf("Data reading started.....\n");
ch=fgetc(fp);
while(ch!=EOF)
{
s++;
ch=fgetc(fp);
}
printf("Total character into file %d",s-1);
getch();
}