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

后端 未结 2 783
暗喜
暗喜 2021-01-29 10:21

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 per

2条回答
  •  萌比男神i
    2021-01-29 11:00

    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.

提交回复
热议问题