Sei sulla pagina 1di 29

‘C’ PROGRAMMING LANGUAGE

In 1970 Dennis Ritch and Brain Kernighan were both discovered the language ‘C’. It is a
general-purpose structure programming language.

‘C’ language is a Basic combining programming language (BCPL). It is also called as middle
level language. There are many special operators in ‘C’ which is not found in other language.

After the introduction of ‘C’ there are various development in the software development in
the software field ‘C’ was the root cause of many operating systems. It is the First language to in
traduce data types in the programming language.

‘C’ is a structural language it has different places to define link etc and we should follow that
structure.
STRUCTURE OF ‘C’

1. Domination section.
2. Link section
3. Definition section
4. Global declaration section
5. Bulldin function
6. Declaration part
7. Executable part
8. Sub program section

DATA TYPES
1. Primary data types.
(i) Integer - long and short
(ii) Float - double and long double
(iii) Character - singed and unsigned
2. User defined data types
(i) Type define (type def)
(ii) Enumerated data (enum)
3. Derived data types
4. Empty data set:
‘C’ is rich it’s data types it supports town classes of data types which we had seen.

ELEMENTS OF ‘C’ LANGUAGE


1. Keywords.
2. Constants.
3. Variables.
4. Operator’s.
5. Header files.
6. Command lines.
7. Character set.

1. Keywords
There are 32 keywords in ‘C’ language.
There are 48 keywords in ‘C++’ language.
Int anto switch unsigned
Float break static volatile
Char void strnct register
For while const type det
Goto else signed default
Do if union double
Long enum return while

2. Constants
During the execution of the program whose value does not change is called a constant.
Constants

Numeric constant Non numeric constant

Integer Real (or) Character String constant


Floating Point constant

132, 2 12.2, 13.0 ‘A’,’B’ “Anna”, “SDI”

3. Variables
During the execution of the program chose value dose changes is called is variables the
various types of variables are
i) Integer variables
ii) Real variables
iii) Character variables
iv) String variables.

4. Operations
There are eight operations.
i) Arithmetic operations
ii) Logical operations
iii) Conditional operation
iv) Relational operations
v) Assignment operation
vi) Increment (or) Decrement operators
vii) Bit wise operators
viii) Special operators

i) Arithmetic operations:-
They are used for arithmetic calculations is ‘C’. an arithmetic expression which has constants
and variables are connected by arithmetic operators they are
+  Addition
-  Subtraction
*  Multiplication
/  Division
Ex: void main()
{
int a,,b,c,d,e,x;
printf(“%d”,x);
scanf(“%d%d%d%d%d”,&a,&b,&c,&d,&e);
x=(a+b)*(c-d)/e
}

ii) Logical Operators:-


Logical operators are used to combine two or more relational expression the logical operators
are
&&  And
||  or
!  not

Ex:- void main()


{
int x,y,i,n;
scanf(“%d%d”,&x,&y);
for(I=1;I<=n;I++)
{
if(n<6)&&(n>=2)
printf(“%d%d”,&x,&y);
if(n=2)||(n=3)
printf(“%d%d”,x,y)
if(n=3)!(n=6)
printf(“%d%d”,x,y);
}
}

iii) Relational Operators


Relational operators are used to connect two arithmetic expressions they are follows.
<  less then
<=  less then (or) equal to
>  greater then
>=  greater then (or) equal to

==  equal to
!=  not equal to

Ex:- void main()


{
int a=3,b=5;
if((a<=3)||(a= =b))
{
printf(“%d%d”,a,b);
if((a>b)||(a<b)
printf(“%d%d”,a,b);
if((a!=b)||(a>=b)
printf(“%d%d”,a,b);
}}

iv) Assignment operator:-


Assignment operator is used to assign a value to a variable. The important assignment
operator is equal symbol “=”.
Syntax: Variable = constant (or) variable (or) expression
Ex:- void main()
{
int a,b,c;
scanf(%d%d%d”,&a,&b,&c);
a=3;
b=a;
c=(a+b)-5;
printf(“%d%d%d”,a,b,c);
}
v) Increment (or) Decrement operators:-
It is also called an ‘unary operator’ there unary operator increment or decrements the value
contained a variable by,
++  Increment operator
--  Decrement operator

Ex: void main()


{
int a=5,b,c;
b=a++;
c=a--;
printf(“%d%d”,b,c); }

vi) Conditional Operator:-

They are also known as ‘Ternary operators’ as they are three expressions. The two symbols
are? And:
Exp 1: Exp 2: Exp 3:
Ex: void main()
{
int x,y,big,small-2 (x>y),x:y;
big=if(xy)?x:y;
printf(“%d is big”, big);
small=if(xy)?x:y;
printf(“%d is small”,small); }
vii) Bitwise operators:-

Operator operation
& address
• value
size of return the bytes
, Comme
. dot
^ Bitwise exclusive or

viii) Special operators:-


There are special operators in ‘C’ they are used to do some specific operation.
%  modular division
>>  right shift
<<  left shift
~  one’s compliment

5. Header Files:-
‘C’ is rich in library function we can use any library function in any ‘C’ program but we have
to include it at the first.
S.no Header Function Name

1. <stdio.h> gets()
puts()
getchar()
putchar()
scanf()
printf()

2. <string.h> strcot()
strlen()
strcom()
stret()

3. <math.h> log()
sin()

4. <stdlib.h> abs()
pow()
abo()

6. Command lines:-

It is another features of ‘C’ it operators under the control known as preprocessor command
lines and directives. There directives they are,
i) Marco substitution directives
ii) File inclution directives
iii) Compiler control directives
7. Character set:-

Upper case letters  A-Z


Lower case letters  a-z
Digits  0-9
Special characters  *,{,}%,&,=,#,^
White spaces  enter of back space
Formatting  /a, /b, /n, /t
Character  /e, /f, /v

Programming in ‘C’
Start  run  command
C:\>cd turboc3
C:\turboc3>tc
F5Full screen

1. Addition

#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the a,b value\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“The result =%d”,c);
}

2. Subtraction
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the a,b value\n”);
scanf(“%d%d”,&a,&b);
c=a-b;
printf(“The result =%d”,c);
}

3. Multiplication
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the a,b value\n”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“The result =%d”,c); }
4. Division

#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the a,b value\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“The result =%d”,c);
}

5. Circular of area ╥r2

#include<stdio.h>
#include<conio.h>
void main() }
int r,area;
printf(“Enter the area value”);
scanf(“%d”.,&r)
area=3.14*r*r;
printf(“The result%d”,area); }

6. Simple Interest si=p*n*r/100

#include<stdio.h>
#include<conio.h>
void main() }
float p,n,r,si;
printf(“Enter the p,n,r value”);
scanf(“%f%f%f”,&p,&n,&r);
si=p*n*r/100;
printf(“The result %f”, si);
getch() }

7. Five subject marks

#include<stdio.h>
#include<conio.h>
void main() }
float tam,eng,cs,sci,ss,sum,avg;
printf(“enter the five marks\n”);
scanf(“%f%f%f%f%f%f”,&tam,&eng,&cs,&sci,&ss);
sum=tam+eng+cs+sci+ss;
avg=sum/5;
printf(“The result of sum:%f’,sum);
printf(“The result of avg:%f”,avg);
getch(); }
8. Conditions: If conditions

#include<stdio.h>
#include<conio.h>
void main() }
int a,b;
printf(“enter the value for a,b”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“A is greater %d”,a);
else
printf(“B is greater %d”,b);
}

9. Nested if
#include<stdio.h>
#include<conio.h>
void main() }
int a,b,c;
printf(‘Enter the a,b,c value”);
scanf(‘%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“A is greater”);
else
printf(“c is greater”);
}
if (b>c)
printf(“B is greater”)
else if
printf(“C is greater”); }

10. For loop condition

#include<stdio.h>
#include<conio.h>
void main() }
int i,n; Out put
printf(“Enter the value of n”); Enter the n value
scanf(‘%d”,&n) 5
for(i=1;i<=n;i++) 1
{ 2
printf(“%d”,i); 3
printf(“\n”); 4
getch(); 5
}}
11. write a program to basic salary in to true to the keyword to DA Basic of 40%,
HRA is 20%, Basic write a program to calculator gross salary

#include<stdio.h>
#include<conio.h> Output
void main() } Enter the value for basic
float basic, da,hra,gs; 2000
printf(“Enter the value for basic”);
scanf(‘%f”,&basic); The result of Gs: 3200
da=basic*40/100;
hra=basic*20/100;
gs=basic+da+hra;
printf(“grass salary = %f”,gs); }

12. Tax and Net value

#include<stdio.h>
#include<conio.h>
void main() }
float gross,tax,net;
printf(“Enter the gross salary”);
scanf(“%f”,&gross”);
tax = 0.14*gross;
net =gross-tax;
printf(“tax with held :%f”,tax);
printf(“net with held :%f”,net);
getch(); }

13. Else if condition

#include<stdio.h>
#include<conio.h>
void main() }

int bs,hra,gs,da;
printf(“enter the basic salary”);
scanf(“%d”,&bs);
if(bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
if(bs>1500)
hra=1500;
da=bs*98/100;
gs+bs+hra+da;
printf(“the gross salary is=%d”,gs);
getch(); }
14. While condition number of reverse

#include<stdio.h> Output
#include<conio.h> Enter the number
void main() } 12345
int n,rev; 54321
printf(“enter the number”);
scanf(“%d”,&n)
while(n>0)
{
rev=n%10;
n=n%10;
printf(“the revers of %d”,rev);
}}

15. Do while number of reverse

#include<stdio.h>
#include<conio.h> Output
void main() } Enter the number
int n,rev; 12345
printf(“Enter the number”); 5
scanf(“%d”,&n); 4
do 3
{ 2
rev=n%10; 1
n=n%10;
printf(“The revers is=%d”,rev);
}
while(n!=0);
}

16. Vowel’s using Switch statement

#include<stdio.h>
#include<conio.h>
void main() }
char vow;
printf(“Enter the vowels”);
scanf(“%c”,&vow);
switch(vow)
{
case ‘a’:
printf(“A is vowels”);
break;
case ‘e’:
printf(“E is vowels”);
break;
case ‘i’:
printf(“O is vowels”);
break;
case ‘o’:
printf(“O is vowels”);
break;
case ‘u’:
printf(“U is vowels”);
break;
default:
printf(“It is not vowels”);
}}

17. Display grads hra using switch statement

#include<stdio.h>
#include<conio.h>
void main() }
char grade;
printf(“Enter the character”);
scanf(“%c”,&grade);
switch(grade)
{
case ‘a’:
printf(“hra is a 45%”);
break;
case ‘b’:
printf(“hra is a 40%”);
break;
case ‘c’:
printf(“hra is a 55%”);
break;
case ‘d’:
printf(“hra is a 60%”);
break;
default:
printf(“The value is not given of hra”);
}}

18. Display the number of switch statement

#include<stdio.h>
#include<conio.h>
void main() }
char num;
printf(“Enter the num”);
scanf(“%c”,&num);
switch(num)
{
case 1:
printf(“num is one”);
break;
case ‘2’:
printf(“num is two”);
break;
case 3:
printf(“num is three”);
break;
case 4:
printf(“num is four”);
break;
default:
printf(“It is not a number”);
}}

Data types Description Typical memory renumbers

Int integer quantity 2 bytes or one word


Char single character 1 bytes
Float floating – point number (i.e. a number
Continuing a decimal
Pointer / or an exponent 1 word (4 bytes)
Sonble aonble – precision floating point
Number (i.e. mote significant figures
And an exponent which may be larger
In magnitude 2 words (8 bytes)

19. Cube formula 1/3╥r2h3

#include<stdio.h>
#include<conio.h>
void main() }
int r,h,s;
printf(“Enter the r,h value”);
scanf(“%d%d”,&r,&h);
s=1/3*3.14*r*r*h*h*h;
printf(“The s value is %d”,s); }

20. To find the formula 2╥r2


#include<stdio.h>
void main() {
int r,s;
printf(“Enter the r,value”);
scanf(“%d”,&r);
s=2*3.14*r*r;
printf(“The s value%d”,s);
}
21. To find the formula 4╥r2
#include<stdio.h>
void main() {
int r,s;
printf(“Enter the r value”);
scanf(“%d”,&r);
s=4*3.14*r*r;
printf(“The s value%d”,s);
}

22. To find the formula 4a

#include<stdio.h>
void main() {
int a,s;
printf(“Enter the a value”);
scnaf(“%d”,&a);
s=4*a;
printf(“The s value is %d”,s);
}

23. To find the formula a+b+c/2

#include<stdio.h>
void main() {
int a,b,c,s;
printf(“Enter the a,b,c value”);
scanf(“%d%d%d”,&a,&b,&c);
s=a+b+c/2;
printf(“The s value is%d”,s);
}

24. To find the formula √ s(s-a)(s-b)(s-c)


4a
#include<stdio.h>
void main() }
int a,b,c,s,r;
printf(“Enter the a,b,c value”);
scanf(“%d%d%d”,&a,&b,&c);
s=a+b+c/2;
r=sqre[s*(s-a)*(s-b)*(s-c)/4*a;
printf(“%d”,s);
printf(“%d”,r);
}

25. Sin Series


#include<stdio.h>
#include<math.h>
void main()
{
int i,j,n,k;
float sum,nume,fact,x;
printf(“Enter the x $ n value”);
scanf(“%d%f”,&n,&x);
x=x*3.1412/180l;
k=1;
sum=x;
nume=x;
for(i=2;i<=n;i++)
{
fact=1;
k=k+2;
for(j=1;j<=k;j++)
{
fact=fact*j;
}
nume=nume*x*x*(-1);
sum=sum+nume/fact;
}
printf(“sum%f”,sum);
}
Output
Enter the x value= 10
Enter the n value= 90
The result = 1.000000

26. Cos Series

#include<stdio.h>
#include<math.h>
void main()
{
int i,j,n,k;
float sum,nume,fact,x;
printf(“Enter the x $ n value”);
scanf(“%d%f”,&n,&x);
x=x*3.1412/180l;
k=0;
sum=1;
nume=x;
for(i=2;i<=n;i++)
{
fact=1;
k=k+2;
for(j=1;j<=k;j++)
{
fact=fact*j;
}
nume=nume*x*(-1);
sum=sum+nume/fact;
}
printf(“sum%f”,1);
}

Output
Enter the x value = 10
Enter the n value = 90
The result = 0.00000

27. Exp series

#include<stdio.h>
#include<math.h>
void main()
{
int i,s,n,x;
float f;
printf(“Enter the x $ n value”);
scanf(“%d%d”,&n,&x);
s=1;
f=1;
for(i=1;i<=n;i++)
{
s=s*i;
f=f+pow(x,i)/s;
}
printf(“%f”,f);
printf(“%f”,exp(x));
}

Output
Enter the x value = 3
Enter the n value = 3
The result = 13.000000

Arrays

Array is defined in which the save manner as the ordinary variable accept that each array
name must be accompanied by a size specification.

Array is set of the elements and same data type and which is in size.
Syntax:

Storage class data- type array[expression]


1. intx[100] 2. Char text [80] 0-79 3. Static char massage [25]
4. Static float in [12]

#include<stdio.h>
#include<conio.h>
#inlcude<ctype.h>
void main()
{
int n,i;
char k[20];
clrscr();
printf(“Enter the how many time to print your name”);
scanf(“%d”,&n);
printf(“Enter the your name”);
scanf(“%c”,&k);
for(i=0;i<=n;i++)
{
printf(“%c”,k)
}}

Write a program to sort to numbers using arrays in


descending order;

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,j,temp;
int a[10];
printf(“Enter the values”);
scnaf(%d”,&n);
for(i=0;i<=n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<=n;i++)
printf(“%d”,&a[i]);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[i]=temp;
}}
printf(“dis sorted”);
for(i=0;i<=n;i++)
printf(“%d”,a[i]);
}
Write a program to read a line in lowercase to convent
uppercase:-

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main() {
cahr a[80];
int i;
clrscr();
for(i=0;i<=79;i++)
a[i]=getchar();
for(i=0;i<=79;i++)
putchar(toupper ([i]));
}

Write a program to array the 25 numbers are entered the by


board to in array write a program find on how many to then
positive, how many of them negative how many of them even,
how many of them odd:-

#include<stdio.h>
#include<conio.h>
void main() {
int i, odd=0,even=0, positive=0, undef=0;
int negative=0; a[25],n;
clrscr();
printf(“how many numbers”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
scanf(“%d”,a[i]);
for(i=0;i<=n;i++)
{
if(a[i]>0)
positive=positive+1;
else
if(a[i]>0)
nagative=nagative+1;
else
undef=undef+1;
if((a[i]%2)= =0)
even=even+1;
}
else
odd=odd+1;
}
printf(“\n positive %d\n”,positive);
printf(“\n nagative %d\n”, nagative);
printf(“\n even %d\n”,even);
printf(“\n odd%d\n”,odd);
printf(“\nundef(0):%d\n”,undef);
getch() }

Array
Array
One dimensional Two dimensional
array array

exp: int a[80] int [2][2];

Write a program to find to read a line in lower case convent in


upper case

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main() {
cahr letter[10];
int i;
clrscr();
for(i=0;i<=9;i++)
letter[i]=getchar();
for(i=0;i<=9;i++)
putchar(toupper (letter[i]));
}

Output
anbazhagan
ANBAZHAGAN

Write a program to find out number of vowel number of


consonance, present in a given string:-

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main() {
char a[20];
int i=0,vowel=0,consenense=0;
gets(a);
while(a[i]=’\0’)
if(a[i]= =’a’|| a[i]= =’e’|| a[i]= =’i’|| a[i]= =’o’|| a[i]= =’u’) {
vowel=vowel+1;
consenense=consenense+1;
i++;
printf(“%d”,vow);
printf(“%d”,con); }
Write a program to find vowels, consence, word, white space in a
given string:-
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
void main() {
int vow=0,cons=0,i=0,word=1,ws=0;
char a[100];
printf(“Enter The String\n”);
gets(a);
while(a[i]!=’\0’) {
if(a[i]==’a’||a[i]==’e’||a[i]==’i’||a[i]==’o’||a[i]==’u’)
vow=vow+1;
else
if((a[i]>’a’ &&a[i]<’z’))
cons=cons+1;
else
if(a[i]==’ ‘)
word=word+1;
ws=word-1;
i++; }
printf(“vowel%d\n”,vow);
printf(“cons%d\n”,cons);
printf(“word%d\n”,word);
printf(“ws%d\n”,ws); }

Out put
vimal was good
vow : 5
con :7
word : 3
ws :2

Addition Matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[2][2],b[2][2],c[2][2];
printf(“the first mat:\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
printf(“the second mat:\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
printf(“the processing :\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
printf(“a mat:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf(“%5d”,a[i][j]);
printf(“\n”);
}
printf(“b mat:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf(“%5d”,b[i][j]);
printf(“\n”);
}
printf(“the result mat:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf(“%5d”,c[i][j]);
printf(“\n”);
}}

Output
A matrix B matrix
1 2 5 6
3 4 7 8

The result c matrix


C= C=
6 8 1+5 2+6
10 12 3+7 4+8

Multiplication

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main() {
int i,j,a[2][2],b[2][2],c[2][2],k;
clrscr();
printf(“The first mat \n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
scanf(“%d”,&a[i][j]);
}}
printf(“The secnd mat \n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
scanf(“%d”,&b[i][j]);
}}
printf(“A mat\n\n”);
printf(“\n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf(“%5d”,a[i][j]);
}}
printf(“\n”);
printf(“B mat\n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
printf(“%5d”,b[i][j]);
printf(“\n”); }
/*The processing*/
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
c[i][j]=0;
for(k=0;k<2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}
printf(“The result mat \n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
printf(“%5d”,c[i][j]); }
printf(“\n”); }}

Output
A matrix B matrix
1 2 4 5
2 3 6 7
The result
16 19
26 31

Transpose

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,a[10][10],b[10][10],m,n;
clrscr();
scanf(“%d%d”,&n,&m);
printf(“%d%d”,n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf(“%d”,&a[i][j]);
printf(“A MATRIX\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%5d”,a[i][j]);
printf(“\n”); }
printf(“TRANSPOSE MATRIX:\n”);
for(i=0;i<m;i++) {
for(j=0;j<n;j++)
printf(“%5d”,a[j][i]);

printf(“\n”); }
getch(); }

Out put
3
3
33
1
2
3
4
5
6
7
8
9
A matrix
1 2 3
4 5 6
7 8 9
Transpose matrix
1 4 7
2 5 8
3 6 9

Subtraction

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,a[2][2],b[2][2],c[2][2];
printf(“the first mat:\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
printf(“the second mat:\n”);
for(i=0;i<2;i++)

for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
printf(“the processing :\n”);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]-b[i][j];
printf(“a mat:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf(“%5d”,a[i][j]);
printf(“\n”); }
printf(“b mat:\n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
printf(“%5d”,b[i][j]);
printf(“\n”); }
printf(“the result mat:\n”);
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
printf(“%5d”,c[i][j]);
printf(“\n”); }
getch(); }

Output
The first matrix The second matrix
3 4 1 2
5 6 3 4
The result
C= C=
2
4-2
2 2
5-3 6-4

Functions
Definition:

The functions is self-condition program, segment the carry is out some specific and well
define task.
Function or Two types
1. Library function
2. User define function
1. Library function
This are onbuit function
Ex: sequerent rout, sin(x), cos(x), exp(x)
2. User define function

This is function defined are by the user in some program some type operation or calculation
is repeated at may places trough a program.
In as such satiation you have to use define function.
Function is a self-contaion program that carrys out some special well define dask.

Advantages of Function

1. To reduce the reputed sequence of this some code.


2. It provides program sharing.
3. To keep track of what you are doing leant. of a program can be reduce.

Syntax:
Function format (structure)
Function name (argument list)  list of variables
Argument declaration  int, folat, char, string
{

local variable declaration


stat 1;
stat 2;
return (exp restion)
}

1. Function name
It is a name given to a function.
2. Condition
We should not use library function name as function name.
3. Argument
It is list of variables subrated by commos, closed by parentless. Argument variable resources
value input for the calling function.
4. Chartgary function
Chartgary of function three category of function.

1. With no argument and no return value.


2. Function with argument and no return value.
3. Function with argument and return values.

Structures
Definition:

A structure is a collection of one or more variable, which may be a type group together under
a single name.
Student
Ex: consefer the student name
Roll number
Tamil
English
Maths
Total
Average

Deceleration of structure
General firm;-
Struct tag name {
member 1;
member 2;
…………
…………
member n; };

Structure must be define the in terms of its individual member. In this is a required keyword
is a name that iden fifiles structures of this type.
(Structure having this comminations) and number1, number2, …… member or individual
number declaration.

The individual member can be ordinary variables pointers, array or other structure. The
member name with in particular structure must be defied form one another though a number name
can be the same has the name of variable that is defined outside of the structure. A storage class,
however, cannot be assigned to an individual number an individual number cannot be with intlalised
in structure time declaration.

Ones the composition of structure has be defined individual structure time variable declare
has follows.

Storage class struct tag var1, var2, ……… var n, were storage class is an optional storage
class specific, struct is a request keyword tag is a name that after in the required, var 1, var2, ………
var n, or structure variables of type tag.

Ex: A typical declaration as show below

Struct account {
Int acc_no;
Char acc_type;
Char name[20];
Float balance;

This structure is named account [that is the tag accounts]. In contain four numbers and integer
quantity [account no] a single character (ary type) and 20 element character away (name of 20) and
floating point quantity (balance the compassion of this a\c is .

Ex: 1
Account Structure

Acct_no member

Acct_type “

Name 20 “

Balance “

Ex: 2
A typical structure declaration is show below
Struct student {
Char name[20];
Int roll_no;
Int tam,eng,maths;
Int total;
Float avg; };
Student

Name 20

Roll no

Tam,eng,maths,

Total

Avg

We can now declaration structure variable old customer, new customer as follows.

Struct account old customer, new customer thus old customer and new customer are variable
of type account. In after words old customer, new customer structures type variables whose
compassion in indefin by the tag account.

It is possible to compain the declaration at a structure compensation with that of a structure


variable as show bellow.

Storage_class struct tag {


Member1 ;
Member2;
………
member n;
}; var1, var2, ……variable of n

Ex: The following single declaration is aquatints to the two declaration percent in the
previous.

Example:
Struct account
{
int acct_no;
char acct_type;
char name[20];
float balance;
}
old customer, new customer

Write a program to find students information’s using structure

#inlcude<stdio.h>
#inlcude<string.h>
void main() {
struct student a
{
char name[20];

int roll_no;
int age;
}a;
struct student b
{
char name;
int roll_no;
int age;
}b;
clrscr();
printf(“Enter the first Struct\n”);
printf(“Enter the name\n”);
scnaf(“%s”,&a.name);
printf(“Enter the rool_no\n”);
scanf(“%d”,&a.roll_no);
printf(“Enter the age\n”);
scanf(“%d”,&a.age);
printf(“Enter the sceond struct\n”);
printf(“Enter the name\n”);
scnaf(“%s”,&b.name);
printf(“Enter the rool_no\n”);
scanf(“%d”,&b.roll_no);
printf(“Enter the age\n”);
scanf(“%d”,&b.age);
printf(“a.name :%c”,a.name);
printf(“a.roll_no :%d”,a.roll_no);
printf(“a.name :%d”,a.age);
printf(“b.name :%c”,b.name);
printf(“b.roll_no :%c”,b.roll_no);
printf(“b.age :%c”,b.age);
}
Output
Enter the first struct
Enter the name
Anbu
Enter the roll_no
203044
Enter the age
22
Enter the second struct
Enter the name
Anbazhagan
Enter the roll_no
3565
Enter the age
23
Structure account

Account

#include<stdio.h>
#include<conio.h>
void main() {
struct account a
{
char name;
int acctno;
int accttype;
float balance;
}a;
struct account b
{
char name;
int acctno;
int accttype;
folat balance;
}b;
clrscr();
printf(“Enter the first account\n”);
printf(“enter the name\n”);
scanf(“%s”,&a.name);
printf(“Enter the acctno\n”);
scanf(“%d”,&a.acctno);
printf(“Enter the accttype\n”);
scanf(“%d”,&a.accttype);
printf(“Enter the balance\n”);
scanf(“%f”,&a.balance);
printf(“Enter the second account\n”);
printf(“enter the name\n”);
scanf(“%s”,&b.name);
printf(“Enter the acctno\n”);
scanf(“%d”,&b.acctno);
printf(“Enter the accttype\n”);
scanf(“%d”,&b.accttype);
printf(“Enter the balance\n”);
scanf(“%f”,&b.balance);
printf(“\n A First account list\n”);
printf(“a.name:%s”,a.name);
printf(“a.acctno:%d”,a.acctno);
printf(“a.accttype%d”,a.accttype);
printf(“a.balance%3.2f”,a.balance)
printf(“\n B Second account list\n”);
printf(“b.name:%s”,b.name);
printf(“b.acctno:%d”,b.acctno);
printf(“b.accttype%d”,b.accttype);
printf(“b.balance%3.2f”,b.balance)
gectch(); }

Output
Enter the first account
Enter the a name
Anbu
Enter the a acct no
2233
enter the a accttype
223
enter the a balance
3

Enter the second account


Enter the a name
Anbazhagan
Enter the b acctno
4646
Enter the b accttype
3543
Enter the b balance
4

The first account list


Anbu
2233
223
3.00

The second account list


Anbazhagan
4646
3543
4.00

Potrebbero piacerti anche