ProgrammingVery hard
union U { int i; char c[6]; }; printf("%zu", sizeof(union U)); What is printed with 4-byte int and 1-byte char?
- A. 6
- B. 4
- C. 10
- D. 8
Correct answer: D. 8
A union is as large as its largest member, here c[6], and then rounded up to the 4-byte alignment of int, giving 8.
More C Programming questions
- 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 =…
- struct Pair { int a; int b; }; struct Pair p = {.b = 4}; printf("%d %d", p.a, p.b); What is printed?