C provides special data types that allow a programmer to group related data and represent complex information in a program. The two important concepts covered in this module are structures and unions.
C provides special data types that allow a programmer to group related data and represent complex information in a program. The two important concepts covered in this module are structures and unions.
A student record may contain a name, roll number and marks. These related values can be grouped together using a structure.
A structure allows different types of related data to be stored together under one variable name. This makes a program easier to organize and represent real-world records.
A student can have an integer roll number, a character grade, and a floating-point percentage. A structure can group all these values into one student record.
Consider a student database. Information such as Roll Number, Name, Course, and Percentage belongs to the same student. Treating them as one record makes the data easier to manage.
One student record can be represented as: Roll No. = 101, Name = Ravi, Percentage = 82.5.
A structure is a user-defined data type that groups variables of different data types under one name.
Here, Student contains three related members:
rollNo, grade and percentage.
A union is also a user-defined data type that groups different types of members under one name, but its members share the same memory location.
In a union, the members share memory, so only one member is normally used to hold a meaningful value at a time.
| Structure | Union |
|---|---|
| Members have separate memory locations. | Members share the same memory location. |
| Multiple members can hold values at the same time. | Generally, one member is used at a time. |
| Useful for representing complete records. | Useful when memory needs to be shared among alternatives. |
Roll Number = 101
Percentage = 82.5
Write a C program to define a structure for a student and display the student's roll number and percentage.
Define a structure named Student with an integer
roll number and a floating-point percentage.
A structure groups related variables of different data types under one name. A structure variable is then used to store values for those members.
Enter roll number: 101
Enter percentage: 82.5
Roll Number = 101
Percentage = 82.5
A structure is useful when several related values, possibly of different data types, need to be treated as one logical record.
| Concept | Remember |
|---|---|
| Structure | Groups related variables of different data types. |
| Union | Groups members that share the same memory location. |
| Structure Member | A variable declared inside a structure. |
| Structure Variable | Variable used to store a structure record. |
Watch a beginner-friendly introduction to structures and unions in C programming.
A short handwritten-style revision sheet for structures and unions will be provided here.
Use the mind map for quick revision of structure, union, members and memory sharing.