When different arithmetic types are used as operands in
certain types of expressions, standard conversions known as usual
arithmetic conversions are applied.
For example, when the
values of two different integral types are added together, both values
are first converted to the same type: when a short int value
and an int value are added together, the short
int value is converted to the int type. Expressions and operators provides a list of the operators
and expressions that participate in the usual arithmetic conversions.
Conversion ranks for arithmetic types
The
ranks in the tables are listed from highest to lowest:
Rules for
decimal floating-point operands
In a context where an operation
involves two operands, if one of the operands is of decimal floating
type, the other operand cannot be a generic floating type, imaginary
type, or complex type. The compiler performs the usual arithmetic
conversions to bring these two operands to a common type. The floating-point
promotions are applied to both operands and the following rules apply
to the promoted operands:
- If both operands have the same type, no conversion is needed.
- If one operand has the _Decimal128 type, the other operand is
converted to _Decimal128.
- Otherwise, if one operand has the _Decimal64 type, the other operand
is converted to _Decimal64.
- Otherwise, if one operand has the _Decimal32 type, the other operand
is converted to _Decimal32.
Rules for other floating-point operands
In
a context where an operation involves two operands, if either of the
operands is of floating-point type, the compiler performs the usual
arithmetic conversions to bring these two operands to a common type.
The floating-point promotions are applied to both operands. If the
rules for decimal floating-point operands do not apply, the following
rules apply to the promoted operands:
- If both operands have the same type, no conversion is needed.
- Otherwise, if both operands have complex types, the type at a
lower integer conversion rank is converted to the type at a higher
rank. For more information, see Floating-point conversions.
- Otherwise, if one operand has a complex type, the type of both
operands after conversion is the higher rank of the following types:
- The complex type corresponding to the type of the generic floating-point
operand
- The type of the complex operand
For more information, see Floating-point conversions.
- Otherwise, both operands have generic floating types. The following
rules apply:
- If one operand has the long double type, the
other operand is converted to long double.
- Otherwise, if one operand has the double type,
the other operand is converted to double.
- Otherwise, if one operand has the float type,
the other operand is converted to float.
Rules for integral operands
In a context
where an operation involves two operands, if both of the operands
are of integral types, the compiler performs the usual arithmetic
conversions to bring these two operands to a common type. The integral
promotions are applied to both operands and the following rules apply
to the promoted operands:
- If both operands have the same type, no conversion is needed.
- Otherwise, if both operands have signed integer types or both
have unsigned integer types, the type at a lower integer conversion
rank is converted to the type at a higher rank.
- Otherwise, if one operand has an unsigned integer type and the
other operand has a signed integer type, the following rules apply:
- If the rank for the unsigned integer type is higher than or equal
to the rank for the signed integer type, the signed integer type is
converted to the unsigned integer type.
- Otherwise, if the signed integer type can represent all of the
values of the unsigned integer type, the unsigned integer type is
converted to the signed integer type.
- Otherwise, both types are converted to the unsigned integer type
that corresponds to the signed integer type.