0% found this document useful (0 votes)
284 views

Cprogramming Notes

C language was developed by Dennis Ritchie in 1972 at Bell Labs. It is called the "Mother of all programming languages" as many other languages were influenced by its features and design. Some key points about C include its use of compilers like Turbo C and Borland C, its primitive data types like int, float, and char, and its typical program structure involving header files, a main function, and escape sequence characters. C programs can perform input/output using statements like printf and scanf and make use of various operators for arithmetic, relational, logical, and assignment operations.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
284 views

Cprogramming Notes

C language was developed by Dennis Ritchie in 1972 at Bell Labs. It is called the "Mother of all programming languages" as many other languages were influenced by its features and design. Some key points about C include its use of compilers like Turbo C and Borland C, its primitive data types like int, float, and char, and its typical program structure involving header files, a main function, and escape sequence characters. C programs can perform input/output using statements like printf and scanf and make use of various operators for arithmetic, relational, logical, and assignment operations.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

C- LANGUAGE

 C-language is programming languages developed by dennis retchi in 1972 at

(AT &T) Advance telephone & telegram bell laboratory in U.S.

 C-language is also called as “Mother of all programming language”

Before 1960 in computer field we have


i.COBAL
ii.FORTRAN
Those are based on machine language
In 1960 “ALGOL”was developed based on machine languages but it failed.
In 1963 “CPL” was developed based on machine language but it failed
In 1970 “B” was developed based on machine language but it failed
In 1972 “C” was developed based on machine language

 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

Procedure for opening c- languages

Start---- Run----cmd---ok

In command window type the followings

C:\document & setting \admin>CD\


C:\> CD TURBOC2
C:\TURBOC2>TC
Two type of window in c-language

I. Designer window

II. Run time window

Bti
OUTPUT STATEMENT :-

It prints the result on the monitor

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

Alt+f9 compiling program


Ctrl +f9 Run
Alt +f5 Output

2. #inclued<stdi.o>
#inclued<conio.h>
Void main()
{
Clrscr();

Printf(“\t\t\t Bhagyashree computer \n”);

Printf(“\t\t\t vazirbad nanded”);

Getch();

}
OUTPUT
Bhagyashree computer vazirbad nanded

Bti
VARIABLE DECLARATION RULE :-

1.the variable name should not exceed more than 32 characters

2. the variable name should not stands with number (or)any special symbol it

should stands with alphabets only

3.keyword should not be used as a variable name

4.the variable name should be a single word we can not create space between

variable

DATA TYPE OF C- LANGUAGES:-

TYPE FORMAT SPECIFIER USE RANGE SIGE

Int %d Round -32768 2bytes

Float %f decimal 32767 4 bytes

Char %c or %s alphabets 32767 4bytes

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.

 C-operators are classified into no.of categories they are

 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)

1. /*write a program to read 3 numbers then calculate addition and multiplication


using c-language*/

#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;

printf("the addition is=%d",d);

printf("the multiplication is=%d",e);

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;

printf("the addition is=%d",c);

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

statement (at the run time storing)

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;

clrscr();

printf(“enter any two value”);

scanf(“%d%d”,&a,&b);

c=a+b;

printf("the addition is=%d",c);

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();
}

/* basic salary of an eployee is input through the keyboard.the da is 25% of


the basic salary while the HRA is 15% of the basic salary. Provident fund is
deducted at the rate of 10% of the gross salary(bs+da+hra).
program to calculate the net salary.*/

#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;

printf("net salary :Rs.%.2f ",netsal);


getch();
}

Bti
RELATIONAL OPERATORS :

Operators Operation

< Less than

> Grater than

<= Less than or Equal to

>= Grater than or Equal to

== Equal to

!= not Equal to

This operators are used to compare the relation between 2 operands.


The result of relational operators always lead to either
TRUE =1
FALSE =0

#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

AND TABLE OR TABLE


A B AND
F F F A B OR
F F F
F T F
F T T
T F F
T T T T F T
T T T
NOT 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 :-

It assign a value to a variable which represent left hand side

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

Increment oprator is used to increment the value by 1 (++)

Decrement operator is used to decrement the value by 1(- -)

Pre-Increment: ++i i=i+1

Pre-Decrement: - -i i=i-1

Post increment : i++ +1

Post decrement i- - -1

#include <stdio.h>

Main()

Int a=7 , b=8 ,c,d,e,f ;

Clrscr();

c=++a + ++b;

d=- - a - - - b;

e=a++;

f=a+(++b);

Printf(“c=%d\n d=%d\n e=%d\n f=%d”,c,d,e,f);

Getch();

Bti
Output

c= 17

d= -1

e= 7

f= 17

Bti
CONDITIONAL OPERATOR

Syntax : exp1 ? exp2 : exp3

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 :-

o Conditional control statement


o Looping /Iterative (or)Repetitive control statement

 Conditional control statement:- In conditional control statement are executed


depending upon condition
 These involves performing a logical test, these test results in either ture or
false
 The following are the conditional control statement:-
a. If statement
b. If else statement
c. Else if / if else –if statement
d. Nested if –else if statement
e. Switch statement
If statement:-
It is used to execute a statement or group of statement
depending on the condition
It is simple if statement it is also called as one-way branching

Syntax:-

If(condition)
{
Statement block;
}
Next statement

Condition :- it is a logical expression that results in either true or false.


Statement block :- a set of statement (or) compound block
/* program to check the no.is even or not */
main()
{
int a;
clrscr();
printf(" enter any numbers\n");
scanf("%d",&a );
if(a%2= = 0)
{
Printf(“the number is even”);
}
Else
{
Printf(“the number is not even”);
}
Getch();
}
Output
2
The number is even
Bti
If condition with && operator :-

Syntax :-

If(condition 1 && condition 2 && condition)

{
Block1 of c- statements;

Else

Block 2 of c-statements;

 in the above syntax if given all condition proves to be true atometically the

block-1 of c statemt get execute if given condition or all conditions process to

falls automatically the block 2 of c statement gets execute

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

statement ‘x’(skipping the rest of the statement)

When all the ‘n’ conditions are false then the final else containing the default

statement will be executed.

Bti
/*Program to read marks of a student and find the division */

#include<stdio.h>

main()

int a,b,c,avg;

clrscr();

printf("enter your three sub marks");

scanf("%d%d%d",&a,&b,&c);

if(a>=75 && b>=60 && c>=75)

printf("Ais first class");

else

if(a>50 && b<60)

printf("Ais secound class");

else

if(a<35&&b<50)

printf("Ais third class");

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;

/* program to check the no’s are equal or not */

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();

printf("enter any alphabet");

scanf("%c",& ch);

if(ch= ='a'||ch= ='e'||ch= ='i')

printf("given character is vowel");

else

Bti
printf("given character is consonant");

getch();

SWITCH CONDITIONAL STATEMENT :-

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

Write a program to read a number (1-5) and display in word


main()
{
int num;
clrscr();
printf("enter any value");
scanf("%d",&num);

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();

Write a program to read bank details


1.amount deposited 2.amount withdraw than calculate balance and interest using
switch controlled statement
4. main()
{
int ad, aw, bal;
long int intr;
int choice;
clrscr();
printf("enter the deposited amount");
scanf("%d",&ad);
printf("enter the withdraw amount");
scanf("%d",&aw);
printf("enter your chioce");
printf("1.balance\n");
printf("2.interest\n");
printf("3.amount deposited\n");
printf("4.amount withdraw\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
bal=ad-aw;
printf("your bal is=%d",bal);
break;
case 2:
bal=ad-aw;
intr=bal*12*5/100;
printf("your intr is=%d",intr);
break;

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”

There are three types of loops


1.while loops
2.Do-while loops
3.for loop statement

WHILE STATEMENT / WHILE LOOP :-

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

I value i<=10 massage i=i+1

1 1<=10- T Welcome to loop 1+1


2 2<=10-T “ 2+1
3 3<=10-T “ 3+1
4,5,6,7,8,9,10
11 11<=10-F

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 a numbers than calculate sum of a even number


Main()
{
Int num,i=1,sum=0
Clrscr();
Printf(“Enter any value”);
Scanf(“%d”,& num);
While(i<=num)
{
Sum =sum+I;
Bti
I=i+1;
}
Printf(“sum of a given numbers=%d”,sum);
Getch();
}
output
I value i<=sum sum=sum+I i=i+1
1 1<=5-T 1=0+1 1+1=2
2 2<=5-T 3=1+2 2+1=3
3 3<=5-T 6=3+3 3+1=4
4 4<=5-T 10=6+4 4+1=5
5 5<=5-T 15=10+5 5+1=6
6 6<=5-F - -

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

Write a program to read 3 digits numbers then print it in revers order


main()
{
int num,k=0;
clrscr();
printf("enter any value");
scanf("%d",&num);
while(num>0)
{
k=k*10+num%10;
num=num/10;
}
printf("num is reverce order=%d",k);
getch();
}

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

Write a program to count the numbers length


main()
{
int i,n;
clrscr();

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);

Difference while & do loops

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();
}

Factorial of a given numbers :-

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();
}

FOR LOOP STATEMENTS :-

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

For(intial variable = value ; condition ; increment /decrement )

{
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();
}

/* write a c- program to print the following out put

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”);
}
}

/*write a program to print the following output


1
11
111
1111
11111*/
#include<stdio.h>
Main()
{
Int i,j,k;
Clrscr();
For(i=1;i<=5;i++)
{
For(k=5;k>=I; k--)
Printf(“ “);
For(j=1;j<=I;j++)
{
Printf(“1”);
}
Printf(“\n”);
}
}

/*write a program to print the following output


*
**
***
****
*****

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”);
}
}

/*write a program to print the following output


1
11
121
1231
12341
*/

#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”);
}
}

/*write a program to print the following output


1
22
333
4444
55555*/

#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”);
}
}

/*write a program to calculate table

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();
}

/* write a ‘c’program to find the given number is prime or not */

#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 :-

Sometime, it’s necessary to immediately from a loop as soon as the condition is


satisfied. When break statement is used inside a loop, then it can cause to terminate
from a loop. The statement after break statement are skipped.
Syntax :

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:

/* Program to demonstrate continue statement.

#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 :

It is a well known as ‘jumping statement.’ It is primarily used to transfer


the control of execution to any place in a program It is useful to provide
branching within a loop.
When the loops are deeply nested at that if an error occurs then it is
difficult to get exited from such loops.Simple break statement cannot
work here properly. In this situations. Goto statement is used.

Syntax :
goto [expr];
figure :
while {condition }
{

For(; ; ; )
{
----------------
-------
Goto err;
-------------------
--------------------
Bti
Err :
}

* program to demonstrate got statement */


#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,j;
clrscr();
while(i<=3)
{
for(j=1;j<=3;j++)
{
printf("*");
if(j==2)
goto stop;
}
i=i+1;
}
stop:
printf("\n\n exited !");
getch();
}

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

Definition of array :- an array is a group of related data items that share a


common name or an array is collection of homogeneous element .
All these elements are stored in consecutive memory location the array contains
all integers or all characters or all float values
 The data type indicates the type of values in an array it may be any four basic
data type [ int, float, char,double]
 Array name is an valid identifier
 Size refers to max elements in an array
 Size must be integer constant, enclosed with in square brackets.
 Each individual elements in an array
 The subscript indicates the position of
 In array subscript starts from ‘0’
 The array is also called as subscripts variable
 The name of an array indicates starting address of an array
Bti
 Arrays are used to store the number of values instead of declaring the numbers
of variables with the help of arrays we can declare the number of variable at a
time
MEMORY ALLOCATION :-

A[0] A[1] A[2] A[n]


A[i]
Element 5 7 2 12

Address 1000 1002 1004 n

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

Write a program to read 10 values and displays then using arrase

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();

printf("enter any 10 values");

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

TWO DIMESNSIONAL ARRAY :-

Ordinary array are indexed by a single integer

Also useful, particularly in numerical and graphics applications is the concept of a

multi-dimensional array, in which we index into the array using an ordered list

MEMORY ALLOCATION :-

Row

A[0][0] A[0][1] A[0][2]


Column a[i][j]
A[1][0] A[1][1] A[1][2]

A[2][0] A[2][1] A[2][2] element

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)

USER DEFINED 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 :-

/* program to demonstrate function.

#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 :

Enter any 2 numbers : 23 6


Addition is : 29

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.

Function Call by passing value :-

When a function is called by passing value of variables then that function is


known as ‘function call by passing values’

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:

/* program to demonstrate function call by passing value.

#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 :-

/* program to demonstrate function call by returning value.

#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

Function call by passing and returning value :


When a function passed and returns value of variables then that function is known as
‘function call by passing and returning values.

Program :

/* program to demonstrate function call by passing

#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
}

/* program to demonstrate function recursion.

#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:

 There should be at least one if statement used to terminate recursion.


 It does not contain any looping statements.

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 :

 It can be applied to calculate factorial of a number, fibonancci series

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.

Function of storage class :

To Detemine the location of a variable where it is stored ?

Set initial value of a variable or if not specified then setting it do default value.

Defining scope of a variable.

To determine the life of a variable.

Types of storage classes:

Storage classes are categorized in 4 (four) types as,

 Automatic Storage Class


 Register Storage Class
 Static Storage Class
 External Storage Class

Automatic Storage Class :


 Keyword:auto
 Storage Location : Main memory
 Initial Value : Garbage Value
 Life : Control remains in a block where it is defined.
 Scope : Local to the block in which variable is declared

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 :

register [data_type] [variable_name];

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.

It is not applicable for arrays, structures or pointer

Bti
Unary and address of (&) cannot be used with these variables as explicitly or
implicitly

Program :-

/* program to demonstrate register storage class.

#include <stdio.h>
#include<conio.h>

Void main( )
{

Register int i=10;


Clrscr( );
{
Register int i=20;
Printf(“\n\t %d”,i);
}
Printf(“\n\n\t %d”,i);
Getch( );
}

Output
20

10

Static storage class :

 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 :

Static [data_type] [variable _ name];

Example :
Stactic int a;

There are two types of static variables as :

a) Local static variable


b) Global static variable

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

Automatic variable value : 2 static variable value : 2

Automatic variable value : 2 static variable value : 3

Automatic variable value : 2 static variable value : 4

External storage class :

 Keyword : extern
 Storage location : main memory
 Initial value : zero
 Life : until the program ends.
 Scope : Global to the program.

Bti
Syntax :

Extern [data_type] [variable _ name];

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

/* program to demonstrate external storage class .

#include <stdio.h>
#include<conio.h>

Extern int i=10;


Void main( )
{
Int i=20;
Void show ( void);
Clrscr( );
Printf(“\n\t %d”,i);
Show( );
Getch( );
}
Void show (void){
Printf(“\n\n\t%d”,i);
}
Output

20
10

PRE-DEFINED FUNCTION :-

1.strlen(string)-this function is used to calculate the lenth of a given string

2.strupr(string)-this function is used to convert into upper case..

3.strulwr(string)- this function is used to convert into lower case

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();
}

/* program to camapare two strings without using strcmp() */

#include<stdio.h>

#include<string.h>

Bti
main()

char string1[5],string2[20];

int i,temp=0;

clrscr();

printf("\n enter the string1 value:\n");

gets(s1);

printf("\n enter scound string:");

gets(s2);

result=strcmp(s1,s2);

if(result ==0)

printf("\n both strings are equal");

else

printf("\n both strings are not equal");

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 :

To read a string, we can use scanf( ) function with format specifier %s

Char name[50];

Scanf(“%s”, name);

The above format allows accepting only string only string which does not have any

black space, tab, new line, form feed, carriage return.

Write strings ;

To write a string , we can use printf( ) function with format specifier %s.

Char name [50];

Scanf(“%s”,name);

Printf(“%s”,name);

STRING.H HEADER FILE :

Download string.h header file :

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 )

Structure is a collection of elements with different data types .

Syntax :-

Struct structure __name


{
Datatype1 vari1,vari2,varin;
Datatype1 vari1,vari2,varin;
Datatype1 vari1,vari2,varin;
};

Struct structure –name obj1,abj2,abj n;

Write a program to read books details with the following feals

s.no book name auther name book price

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();
}

/*program to implement structure**/

#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);

printf("\nstudent name\t roll no\t marks\n");


printf("%s\t\t %i\t\t%f",s1.name,s1.rollno,s1.marks);
printf("\n");
printf("%s\t\t %i\t\t%f",s2.name,s2.rollno,s2.marks);
getch();
}

/*program to implement structure**/

#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");

printf("\nstudent name\t roll no\t marks\n");


printf("%s\t\t %i\t\t%f",s1.name,s1.rollno,s1.marks);
printf("\n");
printf("%s\t\t %i\t\t%f",s2.name,s2.rollno,s2.marks);
getch();
}

Write a program to head student details with the following fields


Rno.sname m,p,c with two read

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();

}
}

Write a program to read consumer details with the following field

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

to pointer variable using pointers

main()

int a,b;

int *ptr;

clrscr();

printf("enter any two values");

scanf("%d%d",&a,&b);

printf("the value of a is=%d\n",a);

printf("the address of a is=%u\n",&a);

printf("the value of a is=%d\n",b);

printf("the address of a is=%u\n",&b);

ptr=&a;

printf("the stored address is=%u\n",fptr);

printf("the address of ptr values=%u\n",&ptr);

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

V is stores value and p stores address pointer variables are initialized by


P=&v;
It indicates ‘p’ holds the starting address of integer variable of v
main()
{
int v=100;
int *p;
p=&v;
clrscr();
printf("%d\n",p);
printf("%d",*p);
getch();
}

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 ;-

 pointer can be passed as arguments to a function definition.

 Passing pointer to a function is called “called by reference”

 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.

 In call by reference the actual arguments must be pointer or reference is that

address

Example: Swapping of two numbers

void swap(int *,int *)


void main()
{
int a=10,b=5;
clrscr();
swap(&a,&b);
printf("a=%d\t b=%d",a,b);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Output
A=15
B=10

Bti
POINTER AND ARRAYS

 Pointer can be used with array for efficient programming .


 The name of an array itself indicates the starting address of an array or address
of first element of an array.
 That means array name is the pointer to the starting or first element of an array
 It ‘A’ is an array the address of first element can be expressed as &A[0] or A
or A+0
 The compiler defines array name as a constant pointer to the first element

For :- th element --- &A[i] or A+i

0 value :- [0] or *(A+0)

Example :Passing each element as an argument and displaying the elements

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.

It allows programmers to separate functions of a program into reusable code or file. It


contains declarations of variables, subroutines. if we want to declare identifiers in
more than one source code file then we can declare such identifiers in header file.
Header file has extension like ‘*.h’ the prototypes of library functions are gathered
together into various categories and stored in header files.
E.g.All prototypesof standard input /output functions are stored in header file
‘stdio.h’while console input /output functions are stored in’conio.h’

The header files can be defined o declared in two ways as

Method 1: #include “header _file-name”


Method 2 : # include<header_file-name>

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.

Standard Header Files :

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 :

/* program to demonstrate math.h header file working.

#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 root of 16 : 4.0000000

Square of 4 : 16.000000

Bti
Sine of 10 : -0.544021

Graphic.h header file:


The ‘graphics.h’ header file is used to declare graphics functions. It declares
prototypes for the graphics functions.

Some of the standard member functions of graphics.h header files are,

/*program to demonstrate graphics.h header file working.

#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

You might also like