ProgrammingHardHCL · Tech Mahindra pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Mixed { short s; char c; int i; };
struct Reordered { int i; short s; char c; };
printf("%zu %zu", sizeof(struct Mixed), sizeof(struct Reordered));- A. 8 8
- B. 12 8
- C. 8 12
- D. 12 12
Correct answer: A. 8 8
Mixed: s@0 (2), c@2 (1), 1 padding byte so i aligns to 4, i@4 (4): total 8. Reordered: i@0 (4), s@4 (2), c@6 (1), 1 trailing padding byte to reach a multiple of 4: total 8. Reordering doesn't always save space; here both layouts happen to total 8 bytes.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct S { int a; int b; }; struct S arr[3] = {{1,2},{3,4},{5,6}}; int p = (int )arr…
- Assume a 64-bit LP64 ABI. What is printed? c struct A { int x; }; struct B { struct A a; }; struct B b1 = {{10}}; struct B b2 = b1; b2.a.x…
- Assume a 64-bit LP64 ABI. What is printed? c struct S { double d; char c; }; struct S arr[2]; printf("%zu %ld", sizeof(arr), (char…
- Assume a 64-bit LP64 ABI. What is printed? c union Shape { struct { double radius; } circle; struct { double width, height; } rect; }…
- Assume 32-bit int and two's complement. What is printed? c int x = 5; int y = ~x + 1; printf("%d", y);
- Assume 32-bit int. What is printed? c int x = 12; printf("%d %d %d", x & 1, x | 1, x ^ 1);