Promotion, Conversion and Casts
Promotion
charorshortvalues (signedorunsigned) are promoted toint(orunsigned) before anything else happensthis is done because
intis assumed to be the most efficient integral datatype, and it is guaranteed that no information will be lost by going from a smaller datatype to a larger oneexamples:
even if
chis achar, the'z'value inch = 'z'is converted to datatypeintbefore being assigned tochas a more graphic illustration,
sizeof('z')returns 4 (on a machine with 8 bit bytes and 32 bitints)similarly, if
stuffis ashortandais achar,stuff = acauses the value fromato be promoted tointpromotion doesn't change the size of a variable:
{ char ch; int before, after; ch = 'a'; before = sizeof(ch); ch = ch + 1; after = sizeof(ch); }sets both
beforeandafterto 1 even though, when evaluatingch = ch + 1, the value ofchis promoted tointbefore adding1
Conversion
after integral promotion, the arguments to an operator are checked
if both are the same datatype, evaluation proceeds
if the arguments are of different datatypes, conversion will occur:
otherwise, if either operand is a
long double, the other operand is converted tolong doubleotherwise, if either operand is a
double, the other operand is converted todoubleotherwise, if either operand is a
float, the other operand is converted tofloatotherwise, if either operand is an
unsigned long, the other operand is converted tounsigned longotherwise, if one operand is a
longand the other isunsigned, one of two things can happen:if
sizeof(long) == sizeof(unsigned)(and thereforelonghas a maximum value less than the maximum value of anunsigned, then both operands are converted tounsigned longif
sizeof(long) > sizeof(unsigned), theunsignedoperand is converted tolong
otherwise, if either operand is a
long, the other operand is converted tolongotherwise, if either operand is an
unsigned, the other operand is converted tounsignedotherwise, both operands are evaluated as
ints
conversion spreads from left to right to raise an expression to the largest datatype:
in an entirely
intcontext,40 / 17 * 13 / 3would evaluate to8(40 / 17rounds to2,2 * 13 = 26,26 / 3rounds to8)to evaluate
40 / 17 * 13 / 3.040 / 17again rounds to22 * 13 = 26but the
3.0forces the final division into adoublecontext and thus26.0 / 3.0 = 8.666...
if we move the decimal point to the
13(40 / 17 * 13.0 / 3), the result will still be8.666...because:40 / 17rounds to2the
13.0forces the multiplication to adoublebut2.0 * 13.0still equals26.0and the
26.0still forces the final division into adoublecontext and26.0 / 3.0 = 8.666...
if we move the decimal point to the
17(40 / 17.0 * 13 / 3), the result now becomes10.196...because:the
17.0forces the initial division into adoublecontext and40.0 / 17.0 = 2.352...2.352... * 13.0 = 30.588...and
30.588... / 3.0 = 10.196...
Casts
the type of an expression can be forced using casts.
a cast is simply any valid datatype enclosed in parentheses and placed next to a constant, variable or expression
examples:
(float)11 / 3forces the entire expression to be evaluated in a floating point context, producing a result of3.6666...to evaluate
((int)7.5 * 2) / (long double)5first, the cast forces
7.5to anint 7the multiplication is done in an integer context yielding
14the cast forces
5to along double 5.0which forces the division to be done in a
long doublecontextleading to a final result of
14.0 / 5.0or2.8
note that casts (as in other conversions) only force conversion at their level, so
(float)(40 / 17 * 13 / 3)would still evaluate to8.0because the entire expression inside the parentheses takes place in anintcontext, after which it is cast to afloat

