ProgrammingHardWipro · Cognizant pattern

Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed?

Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed?
#pragma pack(1)
struct Packed { char c; int i; char c2; };
#pragma pack()
printf("%zu", sizeof(struct Packed));
  1. A. 6
  2. B. 9
  3. C. 8
  4. D. 12

Correct answer: A. 6

pack(1) forces every member to be placed with 1-byte alignment and removes all padding: c@0 (1), i@1 (4, unaligned but packed tightly), c2@5 (1) — total exactly 6 bytes, versus 12 with natural alignment.

More C Programming questions

Exams that ask this topic