Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.