Confusing if else | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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