ProgrammingHardIBM · Deloitte pattern

Assume unsigned int bitfields pack into 32-bit allocation units (typical gcc/Linux x86-64 behavior). What is printed?

Assume unsigned int bitfields pack into 32-bit allocation units (typical gcc/Linux x86-64 behavior). What is printed?
struct BF { unsigned a:3; unsigned b:13; unsigned c:16; };
printf("%zu", sizeof(struct BF));
  1. A. 8
  2. B. 4
  3. C. 12
  4. D. 3

Correct answer: B. 4

a, b and c total exactly 3+13+16 = 32 bits, the width of one unsigned allocation unit, so all three pack into a single 4-byte unit with no spare bits and no second unit is ever started.

More C Programming questions

Exams that ask this topic