0

How hello c printed?

#include <stdio.h> int main() { float a=0.7; if(a<0.7) printf("hello c"); else printf("hello c++"); return 0; }

30th Mar 2021, 2:12 PM
Sathish Kumar M
Sathish Kumar M - avatar
3 Answers
+ 5
Any floating point value by default is of type double unless you give it a type. So a is float and 0.7 is a double. Double has higher precision when compared to float. This is the reason behind the output.
30th Mar 2021, 2:41 PM
Avinesh
Avinesh - avatar
+ 4
Sathish Because: Decimal 1. 0.7 cannot be represented in binary exactly Type of 0.7 is double, the value is: 0.1011001100110011001100110011001100110011001100110011b 2. By assigning double 0.7 into float variable you lose precision, the value is now: 3. 0.101100110011001100110011b Comparing float with double promotes float to double, i.e. the program executes: 0.101100110011001100110011b < 0.1011001100110011001100110011001100110011001100110011b 3. As you can see the numbers differ, the right hand operand is bit greater than the left hand one To get ā€œ2ā€ as output you need to replace 0.7 with 0.7f which says to compiler that you mean float. I hope this helps. Thank You ā˜ŗļøšŸ˜ŠšŸ˜Š Happy Coding Anurag
30th Mar 2021, 2:22 PM
Anurag Kumar
Anurag Kumar - avatar
30th Mar 2021, 2:16 PM
[bool left=True;]
[bool left=True;] - avatar