What is printed by this C code: union U { int i; char c; }; printf(“%zu”, sizeof(union U));?
Correct answer A. 8
Explanation
All members of a union share the same storage, so its size is at least that of the largest member, here 6 bytes for c. The union must also be aligned for int (4 bytes on typical Linux and macOS systems), so the size is rounded up to the next multiple of 4, which is 8. 10 would be the total of both members, which applies to a struct, not a union.
Answer checked How we write and check questions
Discussion