Sometimes a variable must hold a value that is one of several types, but only one at a time: a payment that is either a card number or a cheque number, for example. A structure would reserve space for all the alternatives. A union reserves space for only the largest, and all its members share it.
What a union is
Syntax
union tag { data-type member1; data-type member2; ...};
A union is declared and accessed exactly like a structure (with the dot operator), with the keyword union instead of struct. Storing a value in one member overwrites whatever was in the others.
Memory of struct s { int x; char y; float z; } and union u { int x; char y; float z; }
struct s: | x x x x | y . . . | z z z z | 12 bytes: each member has its own placeunion u: | x x x x | 4 bytes: x, y and z all start here
Declaring, initialising and accessing a union
A union can be initialised in its declaration, but the brace list sets only its first member: union temp t = { 65 };. Since C99, another member can be named: union temp t = { .z = 3.5f };.
The member written most recently is the one that should be read. Reading a different member reinterprets the same bytes as another type, which usually gives a meaningless value.
A union with a structure inside
Two views of the same bytes can be useful. In the study material’s date example, a 9-character date string and a structure that splits it into parts share the same storage:
The initialiser fills the first member, complete_date. The structure member overlays the same characters, so its two-character fields pick out the month, day and year. They are not null-terminated strings, so they are printed with a precision: %.2s.
Structure versus union
Aspect
Structure
Union
Keyword
struct
union
Memory
Each member has its own storage
All members share the same storage
Size
At least the sum of the members
At least the largest member
Members valid at one time
All
Only the one stored last
Initialisation
All members
The first member only (or one named member)
Changing one member
Does not affect the others
Overwrites the others
Typical use
A record with several fields
A value that may be one of several types; saving memory
Key takeaways
A union’s members share the same memory; its size is that of the largest member (plus padding).
Only the most recently stored member is valid.
Brace initialisation sets the first member only.
Structures store all members separately; unions store one at a time.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
For members char name[20]; int id; float salary; (4-byte int and float), find the size of a structure and of a union with these members, and explain the difference.
Show worked answer
Structure: each member has its own storage, so the size is 20 + 4 + 4 = 28 bytes (no padding is needed, since 20 is a multiple of 4). Union: all members share one block as large as the largest member, name, so the size is 20 bytes. The structure can hold a name, an id and a salary at the same time; the union can hold only one of them at any moment, and storing a new member overwrites the previous one.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/structures-and-unions/unions/ to read this page.