常用算术转换Usual Arithmetic Conversions

大多数 C 运算符执行类型转换以将表达式的操作数引入常见类型或将较短的值扩展到计算机运算中使用的整数大小。 Most C operators perform type conversions to bring the operands of an expression to a common type or to extend short values to the integer size used in machine operations. C 运算符执行的转换取决于特定的运算符和操作数的类型。The conversions performed by C operators depend on the specific operator and the type of the operand or operands. 但是,许多运算符对整型和浮点型的操作数执行相似的转换。However, many operators perform similar conversions on operands of integral and floating types. 这些转换称为“算术转换”。These conversions are known as "arithmetic conversions." 从操作数值到兼容类型的转换会导致不改变其值。Conversion of an operand value to a compatible type causes no change to its value.

以下汇总的算术转换称为“常用算术转换”。The arithmetic conversions summarized below are called "usual arithmetic conversions." 这些步骤仅应用于需要算术类型的二元运算符。These steps are applied only for binary operators that expect arithmetic type. 目的是为了产生常见类型(它也是结果的类型)。The purpose is to yield a common type which is also the type of the result. 若要确定实际执行的转换,编译器可将以下算法应用于表达式中的二元运算。To determine which conversions actually take place, the compiler applies the following algorithm to binary operations in the expression. 下面的步骤不具有优先级。The steps below are not a precedence order.

  1. 如果任一操作数是 long double 类型,则将另一个操作数转换为 long double 类型。If either operand is of type long double, the other operand is converted to type long double.

  2. 如果未满足上述条件,并且任一操作数是 double 类型,则将另一个操作数转换为 double 类型。If the above condition is not met and either operand is of type double, the other operand is converted to type double.

  3. 如果未满足上述两个条件,并且任一操作数是 float 类型,则将另一个操作数转换为 float 类型。If the above two conditions are not met and either operand is of type float, the other operand is converted to type float.

  4. 如果未满足上述三个条件(所有操作数都不是浮点型),则对操作数执行整型转换,如下所示:If the above three conditions are not met (none of the operands are of floating types), then integral conversions are performed on the operands as follows:

    • 如果任一操作数是 unsigned long 类型,则将另一个操作数转换为 unsigned long 类型。If either operand is of type unsigned long, the other operand is converted to type unsigned long.

    • 如果未满足上述条件,并且任一操作数是 long 类型且另一个操作数是 unsigned int 类型,则将两个操作数都转换为 unsigned long 类型。If the above condition is not met and either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.

    • 如果未满足上述两个条件,并且任一操作数是 long 类型,则将另一个操作数转换为 long 类型。If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long.

    • 如果未满足上述三个条件,并且任一操作数是 unsigned int类型,则将另一个操作数转换为 unsigned int 类型。If the above three conditions are not met, and either operand is of type unsigned int, the other operand is converted to type unsigned int.

    • 如果未满足上述任何条件,则将两个操作数转换为 int 类型。If none of the above conditions are met, both operands are converted to type int.

    以下代码阐释了这些转换规则:The following code illustrates these conversion rules:

float   fVal;  
double  dVal;  
int   iVal;  
unsigned long ulVal;  

dVal = iVal * ulVal; /* iVal converted to unsigned long  
                      * Uses step 4.  
                      * Result of multiplication converted to double   
                      */  
dVal = ulVal + fVal; /* ulVal converted to float  
                      * Uses step 3.  
                      * Result of addition converted to double   
                      */   

另请参阅See Also

C 运算符C Operators