ProgrammingVery hardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Inner { char c; int i; };
struct Outer { int x; struct Inner in; double d; };
printf("%zu %zu %zu", sizeof(struct Inner), offsetof(struct Outer, in), sizeof(struct Outer));- A. 8 8 24
- B. 8 4 16
- C. 9 4 24
- D. 8 4 24
Correct answer: D. 8 4 24
Inner: c@0, i@4 (padded), end 8 (align 4) -> size 8. Outer: x@0 (4 bytes), in's own alignment is 4 so it can start right at offset 4 (no gap), occupying 4-11; d needs align 8, so it's placed at offset 16 (padding 12-15); end 24, already a multiple of 8.
More C Programming questions
- On a typical x86-64 GCC/Clang target, what is printed? c struct F { unsigned a : 20; unsigned b : 20; }; printf("%zu", sizeof(struct F));
- Assume a 64-bit LP64 ABI. What is printed? c struct S { char c; int arr[2]; }; printf("%zu %zu", sizeof(struct S), offsetof(struct S, arr));
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));
- What is printed? c #pragma pack(push, 1) struct Packed { char c; int i; }; #pragma pack(pop) printf("%zu", sizeof(struct Packed));
- Assume a 64-bit LP64 ABI and 1-byte Bool. What is printed? c struct S { Bool b; double d; }; printf("%zu", sizeof(struct S));
- Assume enum's underlying type is int (typical on common compilers) and a 64-bit LP64 ABI. What is printed? c enum Color { RED, GREEN, BLUE…