ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
struct A { char c; int i; char d; };
struct B { int i; double d; };
struct C { char c; double d; char e; };
printf("%zu %zu %zu", sizeof(struct A), sizeof(struct B), sizeof(struct C));
/* typical x86-64: int 4-byte aligned, double 8-byte aligned */- A. 12 16 24
- B. 6 12 10
- C. 12 12 24
- D. 9 16 17
Correct answer: A. 12 16 24
Members are aligned to their size and the struct is padded to a multiple of its largest alignment. A: c (1) + 3 padding + i (4) + d (1) + 3 = 12. B: 4 + 4 padding + 8 = 16. C: 1 + 7 + 8 + 1 + 7 = 24. Reordering members largest-first shrinks C to 16.
More C Programming questions
- What is printed? c char s = "hello"; printf("%c %c %c", s[1], 4[s], ("abc" + 2));
- What is printed? c int x = 10; int p = &x; int q = &p; q = 20; p += 5; printf("%d", x);
- What is printed? c #define MAX(a, b) ((a) > (b) ? (a) : (b)) int i = 3; int r = MAX(i++, 2); printf("%d %d", r, i);
- What is printed? c unsigned int i; int count = 0; for (i = 3; i >= 0; i--) { if (++count == 5) break; } printf("%d %u", count, i); /…
- What is printed? c printf("%d %d %d %d", -17 / 5, -17 % 5, 17 / -5, 17 % -5);
- What is printed? c int i = 5; sizet s = sizeof(i++); printf("%zu %d", s, i); / 4-byte int /