ProgrammingHardWipro · Cognizant pattern

Assume unsigned int bitfields pack into 32-bit units and a 64-bit LP64 ABI. What is printed?

Assume unsigned int bitfields pack into 32-bit units and a 64-bit LP64 ABI. What is printed?
struct N { unsigned a:20; int b; };
printf("%zu", sizeof(struct N));
  1. A. 4
  2. B. 12
  3. C. 8
  4. D. 5

Correct answer: C. 8

a reserves a full 4-byte allocation unit even though only 20 of its 32 bits are used; the remaining 12 bits cannot be shared with a plain (non-bitfield) int. b is placed right after at offset 4, giving a total of 8 bytes.

More C Programming questions

Exams that ask this topic