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; }
4 odpowiedzi
+ 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.
+ 2
@Chaotic
does the f at the end of 0.7f,
mean float?
could you do the same for doubles and decimals?
+ 2
@Chaotic
Thank you,
Also, would " df " work for a double floating point?
+ 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.