ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
/* x86-64 LP64: natural alignment (double 8, int 4, short 2) */
struct X { char a[5]; double d; char e; };
struct Y { char a[5]; int i; short s; };
struct Z { short s; char a[3]; };
printf("%zu %zu %zu", sizeof(struct X), sizeof(struct Y), sizeof(struct Z));
  1. A. 14 11 5
  2. B. 24 16 6
  3. C. 24 12 6
  4. D. 16 16 6

Correct answer: B. 24 16 6

A char array has alignment 1, so only the scalar members force padding. X: a at 0–4, d at 8, e at 16, size rounded up to a multiple of 8 → 24. Y: a at 0–4, i at 8, s at 12, size rounded to a multiple of 4 → 16. Z: s at 0, a at 2–4, rounded to a multiple of 2 → 6. 14/11/5 are the packed sums.

More C Programming questions

Exams that ask this topic