if(a==b)
does not compare types, it compares values.
As @Kerrek SB commented, the value(s) are converted to a common type.
Each a
and b
go though "usual arithmetic conversions" before the comparison.
... the values yielded by operators with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than required by the type. The use of evaluation formats is characterized by the implementation-defined value of FLT_EVAL_METHOD
: C11dr §5.2.4.2.2 9
Conversion to floating point is to float
, double
or long double
depending on FLT_EVAL_METHOD
.
Assuming conversion to float
...:
Otherwise, if the corresponding real type of either operand is float
, the other operand is converted, without change of type domain, to a type whose
corresponding real type is float
. §6.3.1.8 1
So a
converts to a float
with the value of 3.0
before the comparison.
Since the values compare the same, they pass the if(a==b)
.
Note: Conversion can cause issues as not all int
may covert exactly to a float
.