ProgrammingModerateGATE CS · TCS NQT

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

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

Correct answer: A. 12

Member b must sit at an offset that is a multiple of 4, so it starts at offset 8 after 3 bytes of padding. The struct is then 12 bytes long.

More C Programming questions

Exams that ask this topic