ProgrammingVery hardWipro · 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?
#include <stddef.h>
struct M { unsigned a:10; unsigned b:10; char c; };
printf("%zu %zu", offsetof(struct M, c), sizeof(struct M));- A. 4 5
- B. 4 8
- C. 0 8
- D. 4 12
Correct answer: B. 4 8
a and b together use 20 of the 32 bits in one allocation unit, which still occupies the full 4 bytes. c, needing only 1-byte alignment, is placed right after at offset 4. The struct's alignment is governed by unsigned (4), so the 5-byte logical size is padded up to 8.
More C Programming questions
- 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…
- On a little-endian 64-bit machine, what is printed? c union U { int i; struct { short lo; short hi; } s; }; union U u; u.i = 0x00020001…
- Assume unsigned int bitfields pack into 32-bit units and a 64-bit LP64 ABI. What is printed? c struct N { unsigned a:20; int b; }…
- Assume a 64-bit LP64 ABI (8-byte long long, natural alignment). What is printed? c struct LL { char a; long long b; char c; }…
- Assume a 64-bit LP64 ABI. What is printed? c union Val { int i; float f; }; union Val arr[4]; printf("%zu %zu", sizeof(union Val)…
- Assume a 64-bit LP64 ABI. What is printed? c struct K { const int a; int b; }; printf("%zu", sizeof(struct K));