1. Home
  2. Computer Science MCQs
  3. C Programming
  4. On a typical 64-bit system with 4-byte int, what does…

Added 30 Nov -0001

On a typical 64-bit system with 4-byte int, what does sizeof return for struct S { char c; int i; };?

  1. 8, because padding aligns the int to 4 bytes Correct answer
  2. 5, the sum of the two member sizes
  3. 4, because the char is packed inside the int
  4. 16, because every struct is 16-byte aligned

Correct answer

A. 8, because padding aligns the int to 4 bytes

15 views

MCQSEXAM

Explanation

Compilers insert padding so each member starts at an address aligned to its own size, so one byte for the char is followed by three padding bytes before the 4-byte int, making the struct 8 bytes. The members still occupy only 5 bytes of real data; declaring int first and char second moves the padding to the end but still gives 8 bytes, because the struct's size must be a multiple of its strictest alignment. Using #pragma pack or an attribute removes the padding at the cost of slower unaligned access.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs