Monday, August 17, 2015

Usual Arithmetic Conversions

 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.

These conversions are performed on the operands of a binary operator to bring them to a common
type, which is then used as the type of the result:
[1] If either operand is of type long double, the other is converted to long double.
• Otherwise, if either operand is double, the other is converted to double.
• Otherwise, if either operand is float, the other is converted to float.
• Otherwise, integral promotions (§10.5.1) are performed on both operands.
[2] Otherwise, if either operand is unsigned long long, the other is converted to unsigned long
long.

• Otherwise, if one operand is a long long int and the other is an unsigned long int, then
if a long long int can represent all the values of an unsigned long int, the unsigned long
int is converted to a long long int; otherwise, both operands are converted to unsigned
long long int. Otherwise, if either operand is unsigned long long, the other is converted
to unsigned long long.
• Otherwise, if one operand is a long int and the other is an unsigned int, then if a long
int can represent all the values of an unsigned int, the unsigned int is converted to a
long int; otherwise, both operands are converted to unsigned long int.
• Otherwise, if either operand is long, the other is converted to long.
• Otherwise, if either operand is unsigned, the other is converted to unsigned.
• Otherwise, both operands are int.
These rules make the result of converting an unsigned integer to a signed one of possibly larger size
implementation-defined. That is yet another reason to avoid mixing unsigned and signed integers.

No comments:

Post a Comment