The problem is that variable f
defined as having type float
while float constant 0.1
has type double
. Type double
has more precision than type float
. it has more binary digits to represent fraction. So in this statement
float f = 0.1;
there is a truncation.
To get the expected result you should write at least
if (f == 0.1f)
Also that to be sure that there is no rounding the code should look as
#include <stdio.h>
int main( void )
{
float f = 0.1f;
if ( f == 0.1f )
printf("True\n");
else
printf("False\n");
}