I would like to add two clarifications to @Lundin's otherwise excellent answer, regarding example 1, where there are two operands of identical integer type, but are "small types" that require integer promotion.
I'm using the N1256 draft since I don't have access to a paid copy of the C standard.
First: (normative)
6.3.1.1's definition of integer promotion isn't the triggering clause of actually doing integer promotion. In reality it is 6.3.1.8 Usual arithmetic conversions.
Most of the time, the "usual arithmetic conversions" apply when the operands are of different types, in which case at least one operand must be promoted. But the catch is that for integer types, integer promotion is required in all cases.
[clauses of floating-point types come first]
Otherwise, the integer promotions are performed on both operands. Then thefollowing rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsignedinteger types, the operand with the type of lesser integer conversion rank isconverted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater orequal to the rank of the type of the other operand, then the operand withsigned integer type is converted to the type of the operand with unsignedinteger type.
- Otherwise, if the type of the operand with signed integer type can representall of the values of the type of the operand with unsigned integer type, thenthe operand with unsigned integer type is converted to the type of theoperand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer typecorresponding to the type of the operand with signed integer type.
Second: (non-normative)
There is an explicit example cited by the standard to demonstrate this:
EXAMPLE 2 In executing the fragment
char c1, c2;/* ... */c1 = c1 + c2;the "integer promotions" require that the abstract machine promote the value of each variable to
intsizeand then add the twoints and truncate the sum. Provided the addition of twochars can be done withoutoverflow, or with overflow wrapping silently to produce the correct result, the actual execution need onlyproduce the same result, possibly omitting the promotions.
