ProgrammingVery hardHCL · Tech Mahindra pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct A { char a; double d; char b; };
struct B { double d; char a; char b; };
printf("%zu %zu", sizeof(struct A), sizeof(struct B));- A. 16 16
- B. 24 24
- C. 16 24
- D. 24 16
Correct answer: D. 24 16
A: a@0, pad 7 so d@8 (8 bytes, ending at 16), b@16, then pad 7 to round to the double's 8-byte alignment: 24. B: d@0 (8 bytes), a@8, b@9, then pad 6 to reach 16 (next multiple of 8). Reordering the fields shrinks B by 8 bytes.
More C Programming questions
- 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));
- What is printed? c #include <stdio.h> int f(int n) { static int sum = 0; sum += n; if (n == 0) return sum; return f(n - 1); } int…