Programming in C

(Barry) #1

328 Chapter 14 More on Data Types



  1. If either operand is of type _Bool,char,short int,bit field, or of an enumer-
    ated data type, it is converted to int.

  2. If either operand is of type long long int, the other is converted to long long
    int, and that is the type of the result.

  3. If either operand is of type long int, the other is converted to long int, and that
    is the type of the result.

  4. If this step is reached, both operands are of type int, and that is the type of the
    result.
    This is actually a simplified version of the steps that are involved in converting operands
    in an expression.The rules get more complicated when unsigned operands are involved.
    For the complete set of rules, refer to Appendix A, “C Language Summary.”
    Realize from this series of steps that whenever you reach a step that says “that is the
    type of the result,” you’re done with the conversion process.
    As an example of how to follow these steps, see how the following expression would
    be evaluated, where fis defined to be a float,ian int,la long int,and sa short
    intvariable:
    f i + l / s
    Consider first the multiplication of fby i, which is the multiplication of a floatby an
    int.From step 3, you find that, because fis of type float, the other operand,i, is also
    converted to type float, and that is the type of the result of the multiplication.
    Next, the division of lby soccurs, which is the division of a long intby a short
    int. Step 4 tells you that the short intis promoted to an int. Continuing, you find
    from step 6 that because one of the operands (l) is a long int, the other operand is
    converted to a long int, which is also the type of the result.This division, therefore,
    produces a value of type long int, with any fractional part resulting from the division
    truncated.
    Finally, step 3 indicates that if one of the operands in an expression is of type float
    (as is the result of multiplying f
    i), the other operand is converted to type float,
    which is the type of the result.Therefore, after the division of lby shas been per-
    formed, the result of the operation is converted to type floatand then added into the
    product of fand i.The final result of the preceding expression is, therefore, a value of
    type float.
    Remember, the type cast operator can always be used to explicitly force conversions
    and thereby control the way that a particular expression is evaluated.
    So, if you didn’t want the result of dividing lby sto be truncated in the preceding
    expression evaluation, you could have type cast one of the operands to type float,
    thereby forcing the evaluation to be performed as a floating-point division:
    f * i + (float) l / s
    In this expression,lwould be converted to floatbefore the division operation was
    performed, because the type cast operator has higher precedence than the division

Free download pdf