What will be the output of the program?
#include <stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}
Answer: A
Correct answer: Hi
0.7 written in the source is a double, while a is a float, which holds fewer bits. Storing 0.7 in a float loses precision, and when a is promoted back to double for the comparison it is slightly less than the double 0.7. So the condition is true and Hi is printed - the classic warning against comparing floating-point values of different types for equality or order.
Please log in or register to report this question.