ProgrammingHardProduct company OA · GATE CS

Assume 2-byte short, 4-byte int and natural alignment. What is printed?

Assume 2-byte short, 4-byte int and natural alignment. What is printed?
#include <stddef.h>
struct R { char a; short b; char c; int d; };
printf("%zu %zu %zu", offsetof(struct R, b), offsetof(struct R, d), sizeof(struct R));
  1. A. 2 5 9
  2. B. 2 8 16
  3. C. 2 8 12
  4. D. 1 4 8

Correct answer: C. 2 8 12

a sits at 0 and b must start at an even offset, so 1 byte of padding puts b at 2–3. c is at 4, d needs a multiple of 4, so 3 bytes of padding put it at 8–11. The total, 12, is already a multiple of 4. '2 5 9' ignores d's alignment.

More C Programming questions

Exams that ask this topic