Search code examples
cc99c89misra

What is the difference between Integral Promotion and Balancing in C?


What is the difference between integral promotion and balancing. Can we sum up both the rules by saying that any type is converted to atleast int or unsigned int type before performing any operation(except logical operators &&, ||, !) and to a greater type if any of the operand is of type greater than int ?


Solution

  • There are two different things in the standard but none is called balancing:

    If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.58) All other types are unchanged by the integer promotions.

    ....

    6.3.1.8 Usual arithmetic conversions Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result.

    The general idea for operators that are applied to expressions of different type, is that the the operands are converted to the type where there is the less loss in precision. E.g if a is float and b is double, for the sum a + b a is converted to double before the addition. Similar if c is unsigned long it is first converted to double for c + b.