ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI with #pragma pack(1) applied to this struct only. What is printed?
Assume a 64-bit LP64 ABI with `#pragma pack(1)` applied to this struct only. What is printed?
#pragma pack(push, 1)
struct Packed {
char a;
int b;
char c;
};
#pragma pack(pop)
printf("%zu", sizeof(struct Packed));- A. 6
- B. 12
- C. 9
- D. 8
Correct answer: B. 12
pack(1) forces 1-byte alignment for every member, eliminating all padding: a@0 (1), b@1 (4, unaligned but packed), c@5 (1), total exactly 6 bytes with no trailing padding either.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct A { char x; }; struct B { struct A a; char y; }; struct C { struct B b; char z; }…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point p1 = {1, 2}; struct Point p2 = p1; p2.x = 99…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int data; struct Node next; }; printf("%zu", sizeof(struct Node));
- Assume a 64-bit LP64 ABI. What is printed? c struct Mixed { short s; char c; int i; }; struct Reordered { int i; short s; char c; }…
- 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…