ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
union Data {
    int i;
    float f;
};
union Data d;
d.i = 1065353216;
printf("%.1f", d.f);
  1. A. 1065353216.0
  2. B. 1.0
  3. C. 0.0
  4. D. Undefined behaviour: reading f after writing i

Correct answer: B. 1.0

1065353216 is exactly the IEEE-754 single-precision bit pattern for 1.0f. Reading d.f after writing d.i reinterprets those same bits as a float (type punning through a union, which most C compilers, and C11's own wording, treat as well-defined), giving 1.0.

More C Programming questions

Exams that ask this topic