HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 4 Type Conversions

Usual Arithmetic Conversions

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

In many expressions involving two operands, the operands are converted according to the following rules, known as the usual arithmetic conversions. The common type resulting from the application of these rules is also the type of the result. These rules are applied in the sequence listed below.

  1. If either operand is long double, the other operand is converted to long double.

  2. If either operand is double, the other operand is converted to double.

  3. If either operand is float, the other operand is converted to float.

  4. Integral promotions are performed on both operands, and then the rules listed below are followed. These rules are a strict extension of the ANSI “Usual Arithmetic Conversions” rule (Section 3.2.1.5). This extension ensures that integral expressions will involve long long only if one of the operands is of type long long. For ANSI conforming compilation, the integral promotion rule is as defined in Section 3.2.1.1 of the Standard. For non-ANSI compilation, the unsigned preserving promotion rule is used.

    1. If either operand is unsigned long long, the other operand is converted to unsigned long long,

    2. otherwise, if one operand is long long, the other operand is converted to long long,

    3. otherwise, if either operand is unsigned long int, the other operand is converted to unsigned long int,

    4. otherwise, if one operand is long int, and the other is unsigned
      int
      , and long int can represent all the values of an unsigned
      int
      , then the unsigned int is converted to a long int. (If one operand is long int, and the other is unsigned int, and long int can NOT represent all the values of an unsigned int, then both operands are converted to unsigned long int.)

    5. If either operand is long int, the other operand is converted to long int.

    6. If either operand is unsigned int, the other operand is converted to unsigned int.

    7. Otherwise, both operands have type int.

NOTE: In compatibility mode, the rules are slightly different.

Step 1 does not apply, because long double is not supported in compatibility mode.

Step 3 does not apply, because in compatibility mode, whenever a float appears in an expression, it is immediately converted to a double.

In step 4, remember that the integral promotions are performed according to the unsigned preserving rules when compiling in compatibility mode.

© Hewlett-Packard Development Company, L.P.