1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What is sizeof for union U { char c[5]; double d; } on a…

Added 30 Nov -0001

What is sizeof for union U { char c[5]; double d; } on a platform where double needs 8-byte alignment?

  1. 8, because the union is padded up to a multiple of its strictest member alignment Correct answer
  2. 5, because the union occupies only the raw byte count of its largest individual member
  3. 13, because the byte sizes of all the members are added together inside a union
  4. 16, because every union is rounded up to a power of two that doubles the alignment

Correct answer

A. 8, because the union is padded up to a multiple of its strictest member alignment

14 views

MCQSEXAM

Explanation

Union size equals its largest member rounded up to the alignment of the strictest member, so a 5-byte array beside a double occupies 8 bytes with three bytes of tail padding. All members share the same starting address, which is why writing d and then reading c[0] exposes the machine's byte order. Adding member sizes describes a struct, not a union.

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