C | Structure & Union | Question 7
union test { int x; char arr[8]; int y; }; int main() { printf("%d", sizeof(union test)); return 0; } |
chevron_right
filter_none
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
(A) 12
(B) 16
(C) 8
(D) Compiler Error
Answer: (C)
Explanation: When we declare a union, memory allocated for a union variable of the type is equal to memory needed for the largest member of it, and all members share this same memory space. In above example, “char arr[8]” is the largest member. Therefore size of union test is 8 bytes.
Quiz of this Question
Recommended Posts:
- C | Structure & Union | Question 2
- C | Structure & Union | Question 10
- C | Structure & Union | Question 6
- C | Structure & Union | Question 5
- C | Structure & Union | Question 8
- C | Structure & Union | Question 4
- C | Structure & Union | Question 9
- C | Structure & Union | Question 10
- C | Structure & Union | Question 1
- Difference between Structure and Union in C
- Anonymous Union and Structure in C
- Output of C programs | Set 44 (Structure & Union)
- Output of C++ programs | Set 41 (Structure and Union)
- Union in C
- C | Loops & Control Structure | Question 5



