Earlier we have seen that how a variables can hold value and how arrays can hold a number of values of the same data type.These two data types can handle a great variety of situations. But quite often we deal with entities that are collection of dissimilar data types.There we use strcture.
Syntax :-
struct structName { datatye variableName1; datatye variableName2; . . . . datatye variableNamen; }stuctVariable1,stuctVariable2,stuctVariable3;
Stucture Example :-
Lets enter informatin of 3 students and display them
struct student { char name[20]; char dept[20]; int m1,m2,m3; float avg; }s1,s2; void main() { int total; clrscr(); printf("\nEnter Information "); printf("\nFirst Student: "); printf("\nName: "); scanf("%s",s1.name); printf("\nDept: "); scanf("%s",s1.dept); printf("\nMarks in M1: "); scanf("%d",&s1.m1); printf("\nMarks in M2: "); scanf("%d",&s1.m2); printf("\nMarks in M3: "); scanf("%d",&s1.m3); total=s1.m1+s1.m2+s1.m3; s1.avg=total/3; printf("\nSecond Student: "); printf("\nName: "); scanf("%s",s2.name); printf("\nDept: "); scanf("%s",s2.dept); printf("\nMarks in M1: "); scanf("%d",&s2.m1); printf("\nMarks in M2: "); scanf("%d",&s2.m2); printf("\nMarks in M3: "); scanf("%d",&s2.m3); s2.avg=(s2.m1+s2.m2+s2.m3)/3; printf("\nDisplay Information "); printf("\nFirst Student: "); printf("\nName: "); printf("%s",s1.name); printf("\nDept: "); printf("%s",s1.dept); printf("\nMarks in M1: "); printf("%d",s1.m1); printf("\nMarks in M2: "); printf("%d",s1.m2); printf("\nMarks in M3: "); printf("%d",s1.m3); printf("\nAverage Marks : "); printf("%f",s1.avg); printf("\nSecond Student: "); printf("\nName: "); printf("%s",s2.name); printf("\nDept: "); printf("%s",s2.dept); printf("\nMarks in M1: "); printf("%d",s2.m1); printf("\nMarks in M2: "); printf("%d",s2.m2); printf("\nMarks in M3: "); printf("%d",s2.m3); printf("\nAverage Marks : "); printf("%f",s2.avg); getch(); }