ProgrammingModerateIBM · Deloitte pattern

With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)?

With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)?
union U { int i; double d; char c[10]; };
  1. A. 8
  2. B. 16
  3. C. 22
  4. D. 10

Correct answer: B. 16

A union is as large as its largest member (10 bytes for c), rounded up to a multiple of its strictest alignment (8, for double): 16. Adding the sizes (22) is what a struct would roughly need.

More C Programming questions

Exams that ask this topic