ProgrammingVery hardProduct company OA

What is printed?

What is printed?
union U { int i; float f; } u = {3.9};
printf("%d", u.i);
  1. A. 4
  2. B. 3
  3. C. The bit pattern of the float 3.9 read as an int
  4. D. Compilation error

Correct answer: B. 3

A brace initialiser for a union initialises its first member, here the int i. 3.9 is converted to int by truncation, so u.i = 3. The float member is never written, and no type punning takes place.

More C Programming questions

Exams that ask this topic