+ 2

Why this code is printing Hi?

It is printing Hi How it come? #include<stdio.h> int main() { float a = 0.7; if(a < 0.7) printf("Hi"); else printf("hellow") ; return 0; }

19th Feb 2019, 9:11 PM
Talha Razzaq
Talha Razzaq - avatar
5 Respostas
+ 3
Try to use the literal f at the end of the number. Good practice. Example:. a=0.7f; Also in the comparation if (a<0.7f) If you don't use it the compiler will promote a as double when evaluate the conditional expression. Better use f literal with float numbers.
19th Feb 2019, 9:22 PM
Yarhoslav ME
Yarhoslav ME - avatar
+ 3
Different programming language, same answer: https://www.sololearn.com/Discuss/1477626/?ref=app
20th Feb 2019, 6:10 AM
Anna
Anna - avatar
+ 2
Change float to double and it should work.
19th Feb 2019, 9:15 PM
Rowsej
Rowsej - avatar
+ 2
Yeh that's working with double, But why float is not working?
19th Feb 2019, 9:18 PM
Talha Razzaq
Talha Razzaq - avatar
+ 2
It’s because double has more precision than float.
19th Feb 2019, 9:23 PM
Rowsej
Rowsej - avatar