There are various types of data type such as int, char float etc.
For example:
int i; here variable i can store only data of integer type
char c; here variable c can store only data of character type
float f; here variable f can store only data of floting-point type
Variable of each data type can store only single type of data.
But, if we want to store different type of data in a single variable then we use structure variable.
“structure” is user defined data type that allows to store data items of different data type.
Defining a Structure
We can create a structure, by using the struct keyword.
The struct keyword defines a user defined new data type that can hold more than one member of different data type.
Format of structure is as follows
struct structure_name
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
In a C programming Structures are used to represent a record. Suppose you want to store the information of a school student such as : −
- Name
- Class
- Section
- Roll Number
Example:
struct Student
{ char name[50];
int class;
char section[100];
int rollno;
} s1,s2;
Here s1 and s2 are the structure variable.
Another way of creating a structure variable is:
struct Student
{ char name[50];
int class;
char section[100];
int rollno;
}
int main()
{
struct Student s1, s2;
return 0;
}
Example: Write a program to store and print the following information such as name, roll number, class and section of the student using structure.
#includestruct Student { char name[30]; char section[1]; int rollno; int Class; }s1; int main() { printf("Enter the name, rollno, class & section of student"); scanf("%s", &s1.name); scanf("%d", &s1.rollno); scanf("%d", &s1.Class); scanf("%s",&s1.section); printf("\n Student details"); printf("\n Name = %s", s1.name); printf("\n Rollno = %d", s1.rollno); printf("\n Class = %d", s1.Class); printf("\n Section = %s",s1.section); return 0; }
OUTPUT
Enter the name, rollno, class & section of student Aditya // enter by user 23456 // enter by user 12 // enter by user A // enter by user Student details Name = aditya Rollno = 23456 Class = 12 Section = A
Example: Write a program to store and print the following information such as name, roll number, class and section of 2 students using structure.
#includestruct Student { char name[30]; char section[1]; int rollno; int Class; } s1, s2; int main() { printf("Enter the name, rollno, class & section of 1st student"); scanf("%s", & s1.name); scanf("%d", & s1.rollno); scanf("%d", & s1.Class); scanf("%s", & s1.section); printf("Enter the name, rollno, class & section of 2st student"); scanf("%s", & s2.name); scanf("%d", & s2.rollno); scanf("%d", & s2.Class); scanf("%s", & s2.section); printf("\n 1st Student details"); printf("\n Name = %s", s1.name); printf("\n Rollno = %d", s1.rollno); printf("\n Class = %d", s1.Class); printf("\n Section = %s", s1.section); printf("\n 2nd Student details"); printf("\n Name = %s", s2.name); printf("\n Rollno = %d", s2.rollno); printf("\n Class = %d", s2.Class); printf("\n Section = %s", s2.section); return 0; }
OUTPUT
Enter the name, rollno, class & section of 1st student aditya // enter by user 23456 // enter by user 12 // enter by user A // enter by user Enter the name, rollno, class & section of 2nd student rajeev // enter by user 11111 // enter by user 9 // enter by user B // enter by user 1st Student details Name = aditya Rollno = 23456 Class = 12 Section = A 2nd Student details Name = rajeev Rollno = 11111 Class = 9 Section = B
Example: Write a program to add two distances which is in feet and inches using structure.
#includestruct Distance { int feet; float inch; }; int main() { struct Distance d1, d2, sum; printf("Enter 1st distance"); printf("\n Enter feet: "); scanf("%d", & d1.feet); printf("\n Enter inch: "); scanf("%f", & d1.inch); printf("\n Enter 2nd distance\n"); printf("\n Enter feet: "); scanf("%d", & d2.feet); printf("\n Enter inch: "); scanf("%f", & d2.inch); sum.feet = d1.feet + d2.feet; // add feet sum.inch = d1.inch + d2.inch; // add inches while (sum.inch >= 12) // if inch is greater than 12,change inch to feet { ++sum.feet; sum.inch = sum.inch - 12; } printf("\n Sum of distances = %d %f ", sum.feet, sum.inch); return 0; }
OUTPUT
Enter 1st distance Enter feet: 6 Enter inch: 8.8 Enter 2nd distance Enter feet: 4 Enter inch: 7.9 Sum of distances = 13 3.500000