ProgrammingHard
struct G { int x; char y[3]; }; printf("%zu", sizeof(struct G)); What is printed with 4-byte int and natural alignment?
- A. 7
- B. 8
- C. 9
- D. 12
Correct answer: B. 8
x takes offsets 0 to 3, y takes offsets 4 to 6, and the struct is padded to the 4-byte alignment, giving 8.
More C Programming questions
- struct H { short s; char c; }; printf("%zu", sizeof(struct H)); What is printed with 2-byte short and natural alignment?
- union U { int i; char c[6]; }; printf("%zu", sizeof(union U)); What is printed with 4-byte int and 1-byte char?
- struct E { int n; int a[]; }; printf("%zu", sizeof(struct E)); What is printed for a struct with a flexible array member and 4-byte int?
- struct K { int k; }; struct K list[3] = {{3}, {1}, {2}}; int sum = 0; for (int i = 0; i < 3; i++) sum = sum 10 + list[i].k; printf("%d"…
- struct Acc { int bal; }; void add(struct Acc a, int x) { a.bal += x; } struct Acc acc = {10}; add(acc, 5); printf("%d", acc.bal); What is…
- struct Acc { int bal; }; void add(struct Acc a, int x) { a->bal += x; } struct Acc acc = {20}; add(&acc, 7); printf("%d", acc.bal); What…