Can someone please explain the output here?
#include <stdio.h>
int main(void) {
float f = 0.1 ;
printf ("%f\n",f ) ;
if (f == 0.100000) {
printf ("true ") ;
}
else {
printf ("False") ;
}
return 0;
}
output:
0.100000
False
>Solution :
You are trying to compare a float number with a double. The number 0.100000is considered as doubletype unless you specify with a trailing f that it is a float. Thus:
int main(){
float x = 0.100000;
if (x == 0.100000)
printf("NO");
else if (x == 0.100000f)
// THE CODE GOES HERE
printf("YES");
else
printf("NO");
}
You mat refer to single precision and double precision for theoritical details