Usual arithmetic conversions【C语言】

Usual arithmetic conversions【C语言】_第1张图片

The C language converts operands in arithmetic expressions in a predefined order,known as the usual arithmetic conversions.

Step 1:

If either operand is of type long double, the other is converted to long double, and that is the type of the result.

Step 2:

If either operand is of type double, the other is converted to double,and that is the type of the result.

Step 3:

If either operand is of type float, the other is converted to float, and that is the type of the result.

Step 4:

If either operand is of type _Bool, char, short int, int bit field, or of an enumerated data type, it is converted to int, if an int can fully represent its range of values; otherwise, it is converted to unsigned int. If both operands are of the same type, that is the type of the result.

Step 5:

If both operands are signed or both are unsigned, the smaller integer type is converted to the larger integer type, and that is the type of the result.

Step 6:

If the unsigned operand is equal in size or larger than the signed operand, then the signed operand is converted to the type of the unsigned operand, and that is the type of the result.

Step 7:

If the signed operand can represent all of the values in the unsigned operand, the latter is converted to the type of the former, and that is the type of the result.

Step 8:

If this step is reached, both operands are converted to the unsigned type corresponding to the type of the signed type.

Step 4 is known more formally as integral promotion.

你可能感兴趣的:(Usual arithmetic conversions【C语言】)