ProgrammingHardWipro · Cognizant pattern
Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed?
Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed?
#pragma pack(1)
struct Packed { char c; int i; char c2; };
#pragma pack()
printf("%zu", sizeof(struct Packed));- A. 6
- B. 9
- C. 8
- D. 12
Correct answer: A. 6
pack(1) forces every member to be placed with 1-byte alignment and removes all padding: c@0 (1), i@1 (4, unaligned but packed tightly), c2@5 (1) — total exactly 6 bytes, versus 12 with natural alignment.
More C Programming questions
- Assume 64-bit LP64, natural alignment. What is printed? c struct A { int arr[3]; char c; }; printf("%zu", sizeof(struct A));
- What is printed? c int f(int n) { if (n <= 1) return 1; return n f(n - 1); } int main() { printf("%d", f(5)); }
- What is printed? c int f(int n) { if (n == 0) return 0; int x = f(n - 1); printf("%d ", n); return x + n; } int main() { printf("= %d"…
- What is printed? c int depth = 0; void f(int n) { depth++; if (n == 0) return; f(n - 1); } int main() { f(3); printf("%d", depth); }
- What is printed? c void f(int n) { if (n <= 0) return; printf("%d ", n); if (n % 2 == 0) f(n / 2); else f(n - 1); } int main() { f(6); }
- Assume a 64-bit LP64 ABI. What is printed? c int x = 5, y = 10, z = 15; int arr[3] = {&x, &y, &z}; int p = arr; printf("%d %d %d", arr[1]…