Implicit arithmetic conversions work much as expected. In general, if an operator like + or * which takes two operands (a "binary operator") has operands of different types, the "lower" type is promoted to the higher type. More precisely, for each arithmetic operator, the following sequence of conversion rule is applied.
Notice that allchar
andshort
are converted toint
, andfloat
is converted todouble
.Then if either operand is
double
, the other is converted todouble
, and the result isdouble
.Otherwise if either operand is
long
, the other is converted tolong
, and the result islong
.Otherwise if either operand is
unsigned
, the other is converted tounsigned
, and the result isunsigned
.Otherwise, the operands must be
int
, and the result isint
.float
's in an expression are converted todouble
; all floating point arithmetic in C is done in double precision.Conversions take place across assignments; the value of the right side is converted to the type of the left, which is the type of the result. A character is converted to an integer, either by sign extension or not, as described above. The reverse operation,
int
tochar
, is well-behaved - excess high-order bits are simply discarded. Thus inint i; char c; i=c; c=i;the value ofc
is unchanged. This is true whether or not sign extension is involved.If
x
isfloat
andi
isint
, thenx=iandi=xboth cause conversion;float
toint
causes truncation of any fractional part.
double
is converted tofloat
by rounding. Longerint
's are converted to shorter ones or tochar
's by dropping the excess high-order bits.Since a function argument is an expression, type conversions also take place when arguments are passed to functions: in particular,
char
andshort
becomeint
, andfloat
becomesdouble
. This is why we have declared functions arguments to beint
anddouble
even when the funcon is called withcha
r andfloat
.