WBJECA 2025 — Computer PYQ
WBJECA | Computer | 2025Which of the following statements about structures and unions in C is true?
Choose the correct answer:
- A.
In a structure, all members share the same memory location.
- B.
In a union, all members have separate memory locations.
- C.
The size of a union is equal to the size of its largest member.
(Correct Answer) - D.
The size of a structure is always equal to the sum of the sizes of its members, without any padding.
The size of a union is equal to the size of its largest member.
Explanation
Understanding how memory is allocated for user-defined types in C is essential for efficient programming.
Structures (
struct): Each member of a structure has its own unique memory location. The total size of a structure is at least the sum of the sizes of its members, often including additional bytes for memory alignment (padding) to ensure efficient CPU access.Unions (
union): All members of a union share the same memory location. Therefore, a union can only hold the value of one member at a time. The size of the union is determined by the member with the largest size.
We can represent the memory allocation logic mathematically as:
Size of Union=max(size(m1),size(m2),…,size(mn))
Size of Structure≈i=1∑nsize(mi)+Padding
Option (A) is false: This describes a union, not a structure.
Option (B) is false: This describes a structure, not a union.
Option (D) is false: Structures often include compiler-added padding bytes to align data members for hardware optimization.
Therefore, statement (C) correctly describes the behavior of a union in C.
