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.
Answer checked How we write and check questions
Discussion