ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
struct S { double d; char c; };
struct S arr[2];
printf("%zu %ld", sizeof(arr), (char *)&arr[1] - (char *)&arr[0]);
  1. A. 32 16
  2. B. 18 9
  3. C. 32 9
  4. D. 18 16

Correct answer: A. 32 16

struct S: d@0 (8), c@8 (1), then 7 trailing padding bytes so the struct size (16) is a multiple of 8 (double's alignment). An array of 2 such structs is 32 bytes, and consecutive elements are exactly sizeof(struct S) = 16 bytes apart.

More C Programming questions

Exams that ask this topic