0

Why output of the code is c ?

#include <stdio.h> int main(){ float a=0.7; if(a<0.7) printf("c"); else printf("c++"); return 0; }

14th Jun 2017, 4:56 PM
Ninad
Ninad - avatar
4 Answers
+ 2
This is due to the precision of floating point decimals. You need to take into account the epsilon. If you were to print out the value of a you would see that it shows accuracy to about 6 places past the decimal. printf("%f", a); Likewise in your comparison you are not telling the compiler that 0.7 is a float type so double is inferred. Use if(a<0.7f) and it should run as you are expecting.
14th Jun 2017, 5:20 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
@Chaotic does the f at the end of 0.7f, mean float? could you do the same for doubles and decimals?
14th Jun 2017, 5:25 PM
Manual
Manual - avatar
+ 2
@Chaotic Thank you, Also, would " df " work for a double floating point?
14th Jun 2017, 8:50 PM
Manual
Manual - avatar
+ 1
Yes the f tells the compiler that the number is a float type. Without the f an number with a fractional part or floating point will be inferred as a double type. Decimal is not a built in type for c or c++ and would require an additional header file(s) so this doesn't really apply.
14th Jun 2017, 5:47 PM
ChaoticDawg
ChaoticDawg - avatar