ProgrammingVery hardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers, natural alignment). What is printed?
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers, natural alignment). What is printed?
struct Inner { char a; short b; };
struct Outer { char x; struct Inner in; int y; };
printf("%zu %zu", sizeof(struct Inner), sizeof(struct Outer));- A. 4 10
- B. 2 12
- C. 4 16
- D. 4 12
Correct answer: D. 4 12
Inner: a@0, pad 1, b@2, size 4 (align 2). Outer: x@0, pad 1 so in@2 (covers bytes 2-5), y needs 4-byte alignment so it moves to offset 8, ending at 12; struct alignment is 4, so no trailing padding is needed.
More C Programming questions
- Assume unsigned int bitfields pack into 32-bit allocation units (typical gcc/Linux x86-64 behavior). What is printed? c struct BF {…
- 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…