Confusing if else | SoloLearn: Learn to code for FREE!

+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

7/6/2019 12:42:16 PM

Siddharth Jain

6 Answers

New Answer

+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

+3

Siddharth Jain, just double type variables have better accuracy and that's why it'll print "C++".

+2

Siddharth Jain , you are welcome 👍

+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.

+1

thanks 😋 TheWh¡teCat

+1

while comparision is a implicitly type casted to double ? TheWh¡teCat