Practice mode C Language

What is printed by this C code: struct S { char c; int i; char d; }; printf(“%zu”, sizeof(struct S));?

Correct answer D. 12

Explanation

On typical 64-bit Linux and macOS systems int is 4 bytes and must be 4-byte aligned. After char c the compiler inserts 3 padding bytes so i starts at offset 4, d sits at offset 8, and 3 more bytes of trailing padding make the size a multiple of 4, giving 12. Adding the member sizes gives 6, which ignores padding. Ordering members from largest to smallest often reduces it.

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.