ProgrammingHard

For struct R { char c; short s; int i; } with natural alignment (char 1, short 2, int 4), what is sizeof(struct R)?

  1. A. 7
  2. B. 8
  3. C. 12
  4. D. 16

Correct answer: B. 8

c sits at offset 0, one pad byte follows, s is at offset 2 and i at offset 4: 8 bytes, already a multiple of the 4-byte alignment. 7 ignores padding, 12 pads every member to 4 bytes, and 16 rounds to an 8-byte boundary the struct does not need.

More C Programming questions

Exams that ask this topic