+ 2

What is the output? and why? With proper explaination.

// only logic { float a=0.7; if(a<0.7) printf("hi"); else printf("bye"); } // upvote if you liked this. thanks in advance.

28th Aug 2017, 12:56 PM
Vishanth Tiwari
Vishanth Tiwari - avatar
7 Antworten
+ 5
ok thanx for the info i was wrong but what do you mean by "ans"?
29th Aug 2017, 6:13 AM
‏‪Amer Abdulhafiz‬‏
‏‪Amer Abdulhafiz‬‏ - avatar
+ 2
if a is int then ans will be bye but here ans will be hi bez a is of type float. You can check by compiling program. or print this line ::: printf("%.5f %.5f ", 0.7 ,0.7f); you will find difference.
29th Aug 2017, 6:03 AM
Vishanth Tiwari
Vishanth Tiwari - avatar
+ 2
out put for:: printf("%.5f %.5f ", 0.7 ,0.7f); 0.77777 0.69999 for float it read as 0.69999 and for double 0.77777 so at (a<0.7)// it will compare (0.69<0.7 ) which is true so it will give o/p" hi". You can check this code. I hope you guys find this information interesting.
29th Aug 2017, 6:23 AM
Vishanth Tiwari
Vishanth Tiwari - avatar
0
bye
28th Aug 2017, 1:19 PM
RAJESH KAMMARI
0
is this answer correct
28th Aug 2017, 1:19 PM
RAJESH KAMMARI
0
ans is "hi" because a = 0.7 but since it is float it will be read as 0.69
29th Aug 2017, 3:00 AM
Vishanth Tiwari
Vishanth Tiwari - avatar
0
probably "bye" because a is something like 0.70000001. comparing floats is dangerous because floats rarely are completely accurate. read more about floats and how to compare them: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
29th Aug 2017, 6:20 AM
Niclas Änst
Niclas Änst - avatar