ProgrammingVery hardGATE CS · TCS NQT

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

What is printed? Assume int is 4 bytes with 4-byte alignment.
#include <stdio.h>
union U { char c[5]; int i; };
int main(void) {
    printf("%zu", sizeof(union U));
}
  1. A. 5
  2. B. 4
  3. C. 8
  4. D. 12

Correct answer: C. 8

A union is as large as its largest member, rounded up to the alignment of the most aligned member. The five-byte char array is padded to 8, the next multiple of the int alignment 4.

More C Programming questions

Exams that ask this topic