ProgrammingModerate
struct H { short s; char c; }; printf("%zu", sizeof(struct H)); What is printed with 2-byte short and natural alignment?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: C. 4
s is at offset 0 with size 2, c is at offset 2, and the total is padded to the 2-byte alignment, giving 4 bytes.
More C Programming questions
- 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…
- struct Node { int v; struct Node next; }; struct Node head = NULL; for (int i = 1; i <= 3; i++) { struct Node n = malloc(sizeof n); n->v =…