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; }
3 Respostas
+ 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.
+ 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
0
You will get answer here.
https://www.google.com/amp/s/www.geeksforgeeks.org/comparison-float-value-c/amp/