ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
struct Mixed { short s; char c; int i; };
struct Reordered { int i; short s; char c; };
printf("%zu %zu", sizeof(struct Mixed), sizeof(struct Reordered));
  1. A. 8 8
  2. B. 12 8
  3. C. 8 12
  4. D. 12 12

Correct answer: A. 8 8

Mixed: s@0 (2), c@2 (1), 1 padding byte so i aligns to 4, i@4 (4): total 8. Reordered: i@0 (4), s@4 (2), c@6 (1), 1 trailing padding byte to reach a multiple of 4: total 8. Reordering doesn't always save space; here both layouts happen to total 8 bytes.

More C Programming questions

Exams that ask this topic