Usual arithmetic conversions
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:
Operand type |
---|
long double or long double
_Complex |
double or double _Complex |
float or float _Complex |
Operand type |
---|
_Decimal128 |
_Decimal64 |
_Decimal32 |
Operand type |
---|
long long int , unsigned
long long int |
long int , unsigned
long int |
int , unsigned
int |
short int , unsigned
short int |
char , signed
char , unsigned char |
Boolean |
Notes:
|
Rules for 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. 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
- Otherwise, both operands have generic floating types. The following
rules apply:
- If one operand has the
long double
type, the other operand is converted tolong double
. - Otherwise, if one operand has the
double
type, the other operand is converted todouble
. - Otherwise, if one operand has the
float
type, the other operand is converted tofloat
.
- If one operand has the
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.