char
or short
values (signed
or
unsigned
) are promoted to int
(or
unsigned
) before anything else happens
int
is 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 one
ch
is a char
, the 'z'
value
in ch = 'z'
is converted to
datatype int
before being assigned to ch
sizeof('z')
returns 4
(on a machine with 8 bit bytes and 32 bit int
s)
stuff
is a short
and a
is a char
,
stuff = a
causes the value from a
to be promoted to
int
{ char ch; int before, after; ch = 'a'; before = sizeof(ch); ch = ch + 1; after = sizeof(ch); }sets both
before
and after
to 1 even though, when
evaluating ch = ch + 1
, the value of ch
is promoted
to int
before adding 1
long double
, the other operand is converted to long double
double
, the other operand is converted to double
float
, the other operand is converted to float
unsigned long
, the other operand is converted to unsigned long
long
and the other is
unsigned
, one of two things can happen:
sizeof(long) == sizeof(unsigned)
(and
therefore long
has a maximum value less than the maximum value
of an unsigned
, then both operands are converted to
unsigned long
sizeof(long) > sizeof(unsigned)
, the
unsigned
operand is converted to long
long
, the other operand is converted to long
unsigned
, the other operand is converted to unsigned
int
s
int
context, 40 / 17 * 13 / 3
would evaluate to 8
(40 / 17
rounds to 2
,
2 * 13 = 26
, 26 / 3
rounds to 8
)
40 / 17 * 13 / 3.0
40 / 17
again rounds to 2
2 * 13 = 26
3.0
forces the final division into a
double
context and thus 26.0 / 3.0 = 8.666...
13
(40 / 17 * 13.0 / 3
), the result will still be
8.666...
because:
40 / 17
rounds to 2
13.0
forces the multiplication to a double
but 2.0 * 13.0
still equals 26.0
26.0
still forces the final division into a
double
context and 26.0 / 3.0 = 8.666...
17
(40 / 17.0 * 13 / 3
), the result now becomes
10.196...
because:
17.0
forces the initial division into a
double
context and 40.0 / 17.0 = 2.352...
2.352... * 13.0 = 30.588...
30.588... / 3.0 = 10.196...
(float)11 / 3
forces the entire expression to be evaluated
in a floating point context, producing a result of 3.6666...
((int)7.5 * 2) / (long double)5
7.5
to an int 7
14
5
to a long double 5.0
long double
context
14.0 / 5.0
or 2.8
(float)(40 / 17 * 13 / 3)
would still
evaluate to 8.0
because the entire expression inside the
parentheses takes place in an int
context, after which it
is cast to a float