What will be the output of the following: void main() { float a=0.7; if(a<0.7) printf("c++"); else printf("c"); }
Reveal answer
Fill a bubble to check yourself
What will be the output of the following: void main() { float a=0.7; if(a<0.7) printf("c++"); else printf("c"); }
c++
c
Compile time error
None of these
Float 0.7 is stored as 0.699999... due to binary representation. Literal 0.7 in the if condition becomes double (more precise). Comparison: 0.699999 < 0.700000 → true, so c++ prints. This demonstrates floating-point precision issues.
The literal 0.7 in the comparison a < 0.7 is a double, while a is declared as a float. When the float value 0.7 is stored, it gets rounded to the nearest representable float, and that value, once promoted to double for the comparison, is not exactly equal to the double literal 0.7 — it ends up slightly smaller. So a < 0.7 evaluates true and "c++" is printed, illustrating a classic floating-point precision pitfall rather than a logic error.