Why this code printing "hi" ?

float a = 0.7; if(0.7 > a) printf("hi"); else printf("hello");

2/25/2020 7:46:02 AM

Ravi Coder

4 Answers

New Answer


computer are not able to compute exactly. Better to ask for a difference: if 0.7-a > 0.0001


And also, default type for decimal number is double type so 0.7 is here is decimal type.. And you are comparing 0.7 of double higher type with 'a' float variable value of 0.7 which is less.. 0.7 of double, if converted to float 0.7f is slightly less value than 0.7 double in internal representations..


The magic of floating point numbers and the issues that arise when binary code has to deal with decimal numbers.


doube i = 0.7; if(0.7 > i) cout << "hi"; else cout << "hello"; OUTPUT: hello (in c++)