ProgrammingHardProduct-company MCQ pattern

Assume 64-bit LP64, natural alignment. What is printed?

Assume 64-bit LP64, natural alignment. What is printed?
struct A { int i; char c; };
struct B { char c; int i; };
printf("%zu %zu", sizeof(struct A), sizeof(struct B));
  1. A. 8 8
  2. B. 8 5
  3. C. 5 5
  4. D. 5 8

Correct answer: A. 8 8

Struct A: i@0 (4 bytes), c@4 (1 byte), padded to 8 (multiple of 4). Struct B: c@0 (1 byte), padding to align i to offset 4, i@4 (4 bytes), total 8. Despite having the same members, both end up 8 bytes due to padding — reordering doesn't always save space when there are only two members like this.

More C Programming questions

Exams that ask this topic