A union is a user-defined data type in C that allows different members to share the same memory location. A union can contain members of different data types, but at a particular time, the same memory area is used by all its members.
A union is a user-defined data type in C that allows different members to share the same memory location. A union can contain members of different data types, but at a particular time, the same memory area is used by all its members.
Here, number, value, and
grade share the same memory area.
A union is declared using the union keyword, followed
by the union name and its members inside braces.
After defining a union, a union variable is declared using the union type. The variable provides access to the members of the union.
Here, d1 is a variable of type
union Data.
Union members are accessed using the dot operator (.), just like members of a structure.
The most important feature of a union is that all its members share the same memory location. Therefore, assigning a value to one member can affect the value represented by another member.
After assigning d1.value, the same shared memory is
being used for that member. Therefore, a union is normally used
when only one of its alternative members needs to hold a meaningful
value at a time.
The size of a union is determined by the size required for its largest member, subject to the compiler's alignment requirements. Since members share storage, a union can use less memory than a structure containing the same members.
If a union contains an int, a float,
and a char, its storage is large enough to hold
the member requiring the largest amount of storage.
Both structures and unions can contain members of different data types, but their memory arrangement is different. A structure gives separate storage to its members, whereas a union shares the same storage among its members.
| Structure | Union |
|---|---|
| Each member has separate storage. | All members share the same storage. |
| Multiple members can hold meaningful values at the same time. | Normally one member is used to hold the meaningful value at a time. |
| Size is generally based on the total storage of its members and padding. | Size is generally based on the largest member and alignment. |
| Useful for complete records. | Useful when alternative representations share storage. |
A union is useful when a program needs to store one of several possible types of information in the same memory area. It is especially useful when saving memory is important.
A data item may sometimes need to store an integer and at another time a floating-point value. A union allows both alternatives to use the same storage.
Write a C program to define a union containing an integer and a floating-point member. Store a value in each member one at a time and display the value after each assignment.
Create a union named Data with members
number and value. Assign a value to one
member, display it, and then assign a value to the other member.
Unlike a structure, a union provides one shared memory area for all its members. When a value is assigned to one member, that member becomes the currently stored value in that shared area.
Integer value = 10
Float value = 25.5
The program stores one value and then uses the same shared memory for another member. Therefore, a union should be used when the members represent alternative forms of data rather than values that must all be stored simultaneously.
| Concept | Remember |
|---|---|
| Union | User-defined type whose members share memory. |
| Keyword | union |
| Member Access | Use the dot operator. |
| Memory | Members use the same storage area. |
| Main Use | Useful for alternative data that can share storage. |
Watch a beginner-friendly explanation of unions in C programming.
A short handwritten-style revision sheet for unions will be provided here.
Use the mind map for quick revision of union, shared memory, member access and structure comparison.