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.
#includeusing namespace std; struct Student { char name[30]; char section[1]; int rollno; int Class; } s1; int main() { cout << "Enter the name, rollno, class & section of student"; cin >> s1.name; cin >> s1.rollno; cin >> s1.Class; cin >> s1.section; cout << "\n Student details"; cout << "\n Name =" << s1.name; cout << "\n Rollno =" << s1.rollno; cout << "\n Class = " << s1.Class; cout << "\n Section = " << 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.
#includeusing namespace std; struct Student { char name[30]; char section[1]; int rollno; int Class; } s1, s2; int main() { cout << "Enter the name, rollno, class & section of 1st student"; cin >> s1.name; cin >> s1.rollno; cin >> s1.Class; cin >> s1.section; cout << "Enter the name, rollno, class & section of 2nd student"; cin >> s2.name; cin >> s2.rollno; cin >> s2.Class; cin >> s2.section; cout << "\n 1st Student details"; cout << "\n Name =" << s1.name; cout << "\n Rollno =" << s1.rollno; cout << "\n Class = " << s1.Class; cout << "\n Section = " << s1.section; cout << "\n 2nd Student details"; cout << "\n Name =" << s2.name; cout << "\n Rollno =" << s2.rollno; cout << "\n Class = " << s2.Class; cout << "\n Section = " << 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 Sam // enter by user 23423 // enter by user 12 // enter by user B 1st Student details Name = aditya Rollno = 23456 Class = 12 Section = A 2nd Student details Name = Sam Rollno = 99999 Class = 12 Section = B