Type conversions occur explicitly in C and C++ as the result of a cast or implicitly
as required by an operation. While conversions are generally required for
the correct execution of a program, they can also lead to lost or misinterpreted
data. This section describes how and when conversions are performed and
identifies their pitfalls.
Implicit conversions are, in part, a consequence of the C language ability
to perform operations on mixed types. For example, most C programmers
would not think twice before adding an unsigned char to a signed char and
storing the result in a short int. This is because the C compiler generates the
code required to perform the required conversions implicitly.
The C99 standard rules define how C compilers handle conversions. These
rules, which are described in the following sections, include integer promotions,
integer conversion rank, and usual arithmetic conversions.
...
Integer types smaller than int are promoted when an operation is performed on
them. If all values of the original type can be represented as an int, the value of
the smaller type is converted to an int; otherwise, it is converted to an
unsigned int.
Integer promotions are applied as part of the usual arithmetic conversions
(discussed later in this section) to certain argument expressions, operands of
the unary +, --, and ~ operators, and operands of the shift operators. The following
code fragment illustrates the use of integer promotions:
...
Integer promotions require the promotion value of each variable (c1 and
c2) to int size. The two ints are added and the sum truncated to fit into the
char type.
Integer promotions are performed to avoid arithmetic errors resulting from
the overflow of intermediate values. On line 5 of Figure 5--7, the value of c1 is
added to the value of c2. The sum of these values is then added to the value of
c3 (according to operator precedence rules). The addition of c1 and c2 would
result in an overflow of the signed char type because the result of the operation
exceeds the maximum size of signed char. Because of integer promotions,
however, c1, c2, and c3 are each converted to integers and the overall expression
is successfully evaluated. The resulting value is then truncated and stored
in cresult. Because the result is in the range of the signed char type, the truncation
does not result in lost data.
...
Every integer type has an integer conversion rank that determines how conversions
are performed. The following rules for determining integer conversion
rank are defined in C99.
- No two different signed integer types have the same rank, even if they
have the same representation. - The rank of a signed integer type is greater than the rank of any signed
integer type with less precision. - The rank of long long int is greater than the rank of long int, which
is greater than the rank of int, which is greater than the rank of short
int, which is greater than the rank of signed char. - The rank of any unsigned integer type is equal to the rank of the corresponding
signed integer type, if any. - The rank of any standard integer type is greater than the rank of any
extended integer type with the same width. - The rank of char is equal to the rank of signed char and unsigned
char. - The rank of any extended signed integer type relative to another
extended signed integer type with the same precision is implementation
defined but still subject to the other rules for determining the integer
conversion rank. - For all integer types T1, T2, and T3, if T1 has greater rank than T2 and
T2 has greater rank than T3, then T1 has greater rank than T3.
...
The integer conversion rank is used in the usual arithmetic conversions to
determine what conversions need to take place to support an operation on
mixed integer types.
Usual Arithmetic Conversions
Many operators that accept arithmetic operands perform conversions using the
usual arithmetic conversions. After integer promotions are performed on both
operands, the following rules are applied to the promoted operands.
- If both operands have the same type, no further conversion is needed.
- If both operands are of the same integer type (signed or unsigned), the
operand with the type of lesser integer conversion rank is converted to
the type of the operand with greater rank. - If the operand that has unsigned integer type has rank greater than or
equal to the rank of the type of the other operand, the operand with
signed integer type is converted to the type of the operand with
unsigned integer type. - If the type of the operand with signed integer type can represent all of
the values of the type of the operand with unsigned integer type, the
operand with unsigned integer type is converted to the type of the
operand with signed integer type. - Otherwise, both operands are converted to the unsigned integer type
corresponding to the type of the operand with signed integer type.
Specific operations can add to or modify the semantics of the usual arithmetic
operations.
Example
For In the following example, assume the following code is compiled and executed on IA-32:
...