+ 2

Confusing if else

#include"stdio.h" int main() { float a=0.7d; if(a<0.7) printf("C"); else printf("C++"); return 0; } Ans is c But why Please explain in detail

6th Jul 2019, 12:42 PM
Siddharth Jain
Siddharth Jain - avatar
6 Respostas
+ 3
Because floating point numbers has certain accuracy of representation. So in fact a is represented as 0.6999...... That's why a < 0.7 and "C" is printed. Look at the code and the actual value of variable "a". Hope it helps you 😉 https://code.sololearn.com/c0IdhHFKo9A5/?ref=app
6th Jul 2019, 1:03 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 3
Siddharth Jain, just double type variables have better accuracy and that's why it'll print "C++".
6th Jul 2019, 1:20 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Siddharth Jain , you are welcome 👍
6th Jul 2019, 1:07 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Siddharth Jain , in fact if you change the type of variable "a" to double you'll get a better accuracy and then it'll print "C++" on screen.
6th Jul 2019, 1:12 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
thanks 😋 TheWh¡teCat
6th Jul 2019, 1:06 PM
Siddharth Jain
Siddharth Jain - avatar
+ 1
while comparision is a implicitly type casted to double ? TheWh¡teCat
6th Jul 2019, 1:16 PM
Siddharth Jain
Siddharth Jain - avatar