ProgrammingVery hardGATE CS

What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation.

What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation.
#include <stdio.h>
int main(void) {
    float f = 0.1f;
    double d = 0.1;
    printf("%d %d %d", f == d, (float)d == f, f == 0.1f);
    return 0;
}
  1. A. 0 0 1
  2. B. 1 0 0
  3. C. 1 1 1
  4. D. 0 1 1

Correct answer: D. 0 1 1

f == d promotes f to double: the float nearest 0.1 is 0.10000000149..., which differs from the double nearest 0.1, so the result is 0. Rounding d to float gives the same float as 0.1f, and 0.1f == f is trivially 1.

More C Programming questions

Exams that ask this topic