From WikiChip
Difference between revisions of "c/usual arithmetic conversions"
< c

(usual arithmetic conversions)
 
(No difference)

Revision as of 06:48, 4 January 2015

C operators routinely perform type conversions to get its operands to a common type or extend a short values to the natural integer size used by the machine. Usual arithmetic conversions are the steps a translator must take in order to determine the common type for each operator before the operation takes place. The conversions apply only to binary operators that accept real, arithmetic type operands. The type of the result is the type of the common type.

Steps

  1. If either operands is of type long double, the other operand is converted to type long double.
  2. If the condition above is not met and either operands is of type double, the other operand is converted to type double.
  3. If the conditions above are not met and either operands is of type float, the other operand is converted to type float.
  4. If none of the conditions above are met than conversions take place according to the following rules:
    1. If both operands have the same type, then no further conversion is needed.
    2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
    4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.