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. 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 and only if the two operands do not have the same 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.If either operand is of type long double, the other operand is converted to type long double.
2.If the above condition is not met and either operand is of type double, the other operand is converted to type double.
3.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:
A.If either operand is of type unsigned long, the other operand is converted to type unsigned long.
B.If the above condition is not met and either operand is of type long and the other of type unsigned int, the operand of type unsigned int is converted to type long (in 16-bit compilations) or both operands are converted to type unsigned long (in 32-bit compilations).
C.If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long.
D.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.
E.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.a.
* Result of multiplication converted to double
*/
dVal = ulVal + fVal; /* ulVal converted to float
* Uses step 3.
* Result of addition converted to double
*/