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));- A. 8
- B. 4
- C. 12
- 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
- Assume unsigned int bitfields pack into 32-bit allocation units and a zero-width bitfield forces the next field to a new unit. What is…
- On a little-endian 64-bit machine, what is printed? c union U { int i; char c[4]; }; union U u; u.i = 0x41424344; printf("%c%c%c%c"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Variant { int type; union { int i; float f; double d; }; }; printf("%zu"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { char tag; struct Node next; int val; }; printf("%zu", sizeof(struct Node));
- Assume unsigned int bitfields pack into 32-bit units and a 64-bit LP64 ABI. What is printed? c #include <stddef.h> struct M { unsigned…
- Assume a 64-bit LP64 ABI. What is printed? c struct A { char a; double d; char b; }; struct B { double d; char a; char b; }; printf("%zu…