Difference between structures and unions
This tutorial differentiates between the
structures and
unions in C programming language.
Difference Between Structure and Union
Structure |
Union |
A structure is a collection of data members possibly of different data types grouped together under a single name. |
A union is a special type of variable in C which contains data members possibly of different data types, also grouped under a single name. |
A structure variable acts like a record. |
A union variable acts like a radio button which is used to access or store only one out of multiple options. |
All members of structure occupy distinct memory locations. |
All members of union share the same memory location. |
All members of the structure can be initialized at a time. |
Only the first member of union can be initialized. |
All the members of a structure can be accessed and can be assigned some values at a time. |
Only one member of union can be accessed and can be assigned a value at a time. |
The size of the structure variable is the sum of the sizes of individual data members of the structure. |
The size of a union variable is the size of the largest data member. |