ProgrammingModerateIBM · Deloitte pattern

On a typical compiler where int is 4 bytes and 4-byte aligned, what is printed?

On a typical compiler where int is 4 bytes and 4-byte aligned, what is printed?
struct S { char c; int i; char d; };
struct T { int i; char c; char d; };
printf("%zu %zu", sizeof(struct S), sizeof(struct T));
  1. A. 6 6
  2. B. 12 8
  3. C. 12 12
  4. D. 8 8

Correct answer: B. 12 8

In S, c takes byte 0, three padding bytes put i at 4–7, d sits at 8, and trailing padding rounds the size up to a multiple of 4: 12. In T, i is at 0–3, c and d at 4 and 5, then padding to 8. Ordering members from largest to smallest saves space.

More C Programming questions

Exams that ask this topic