Domanda

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 ?

È stato utile?

Soluzione

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.

Altri suggerimenti

"Integral promotions" is the old C90 term, the formal standard term is integer promotions.

Integer promotions is a rule that applies whenever a small integer type (bool, char, short and their signed equivalents) is used as an operand in an expression.

C11 6.3.1.1/4

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. All other types are unchanged by the integer promotions.

"Balancing" is the informal term referring to a set of rules known as the usual arithmetic conversions. They state how all implicit type promotions of each operand in an operation are done. Please note that the integer promotions are part of the usual arithmetic conversions:

C11 6.3.1.8

First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

If both operands have the same type, then no further conversion is needed.

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.

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.

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.

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top