ProgrammingHardProduct-company MCQ pattern
Assume 64-bit LP64, natural alignment. What is printed?
Assume 64-bit LP64, natural alignment. What is printed?
struct A { int i; char c; };
struct B { char c; int i; };
printf("%zu %zu", sizeof(struct A), sizeof(struct B));- A. 8 8
- B. 8 5
- C. 5 5
- D. 5 8
Correct answer: A. 8 8
Struct A: i@0 (4 bytes), c@4 (1 byte), padded to 8 (multiple of 4). Struct B: c@0 (1 byte), padding to align i to offset 4, i@4 (4 bytes), total 8. Despite having the same members, both end up 8 bytes due to padding — reordering doesn't always save space when there are only two members like this.
More C Programming questions
- Assume 64-bit LP64, natural alignment. What is printed? c struct S { double d; int i; }; struct S arr[3]; printf("%zu %zu", sizeof(struct…
- Assume 64-bit LP64. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));
- Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed? c #pragma pack(1) struct Packed { char c; int i; char…
- 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"…