In following code:
float x = 0.23;
int z;
z = x;
if (x)
printf("float %f will not be converted to 0!\n", x);
if (z)
printf("this will not print!\n");
I thought that floating point number will be converted to integer and then checked if it is zero when used in if
statement. Can someone please explain to me, why that is not the case?
edit:
(just to clarify what has been confusing me)
Since logical operators like <
return int
, I thought that if
statement receives
integer value so conversion needs to be done. As pointed out by answers, it doesn't. Thank you everyone!
Copyright Notice:Content Author:「Slaven Glumac」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14999940/conversion-of-float-in-if-statement