ProgrammingHardProduct company OA · GATE CS

Assume 4-byte int with 4-byte alignment. What is printed?

Assume 4-byte int with 4-byte alignment. What is printed?
union U { char c[5]; int i; };
printf("%zu", sizeof(union U));
  1. A. 5
  2. B. 4
  3. C. 9
  4. D. 8

Correct answer: D. 8

A union is as large as its largest member (5 bytes for c) but its size must also be a multiple of its strictest alignment (4, from int), so it is padded to 8. '5' forgets the alignment; '9' adds the members as a struct would.

More C Programming questions

Exams that ask this topic