1. Home
  2. Computer Science MCQs
  3. C Programming
  4. For struct S { char a; int b; char c; }; with 4-byte ints…

Added 30 Nov -0001

For struct S { char a; int b; char c; }; with 4-byte ints and 4-byte alignment, what is sizeof(struct S)?

  1. 12 Correct answer
  2. 6
  3. 9
  4. 16

Correct answer

A. 12

17 views

MCQSEXAM

Explanation

Structure padding inserts three bytes after a so that b starts at offset 4, and the total is rounded up to a multiple of the strictest alignment, giving 12 bytes for offsets 0, 4 and 8 plus tail padding. Reordering members as int, char, char fits the same data into 8 bytes, which matters for large arrays and wire formats. Packing directives change the layout but can cost unaligned access penalties.

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