ProgrammingVery hardProduct company OA
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
struct A { char c; double d; int i; };
struct B { double d; int i; char c; };
struct C { char c; short s; char d; int i; };
struct D { struct C x; char t; double *p; };
printf("%zu %zu %zu %zu %zu", sizeof(struct A), sizeof(struct B), sizeof(struct C), sizeof(struct D), offsetof(struct D, p));- A. 13 13 8 21 13
- B. 24 16 8 24 16
- C. 24 16 12 32 24
- D. 24 16 12 24 16
Correct answer: D. 24 16 12 24 16
Each member is placed at a multiple of its own alignment and the struct is padded to a multiple of its strictest member. A: c@0, d@8, i@16, tail-padded to 24. B: d@0, i@8, c@12, padded to 16 – same members, reordered, 8 bytes smaller. C: c@0, s@2, d@4, i@8 → 12 (alignment 4). D: x occupies 0–11, t@12, then p must be 8-aligned → offset 16, size 24. The 13/13/8/21 option is the packed sum of member sizes.
More C Programming questions
- What is printed? c #define SQ(x) x x #define MIN(a, b) ((a) < (b) ? (a) : (b)) int calls = 0; int next(void) { return ++calls; } int…
- What is printed? c #define N 4 #define STR(x) #x #define XSTR(x) STR(x) #define CAT(a, b) a##b #define XCAT(a, b) CAT(a, b) int NN = 9…
- Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)? c typedef char strt; #define STRM char char…
- What is printed? c int g = 1; int f(int n) { static int s = 10; int g = n; s += g; { extern int g; g += s; } return s; } int main(void) {…
- What is printed? c int i = 0, j = 5, k = 0; int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k); printf("%d %d %d %d", r, i, j, k);
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…