Question

Studying C conversions I found a paragraph about Integer promotions that I haven't understand very well, it says:

"Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to an int; otherwise, it is converted to an unsigned int"

Considering that:

  1. the rank of any unsigned integer type is equal to the rank of the corresponding signed integer type

  2. The rank of long long int is greater than the rank of long int, which is greater than the rank of int, which is greater than the rank of short int, which is greater than the rank of signed char.

the questions are: Why a type smaller than int should not be represented by an int? and why an unsigned int could represent a value that an int should not represent?

Thanks in advance.

Was it helpful?

Solution

Although a short has lower rank than an int, it could be the same size (in bits), e.g. sizeof(int) == sizeof(short) == 2 on 16 bit systems. So an unsigned short may be able to hold a value greater than INT_MAX.

For the second part of your question, which has much the same answer: an unsigned int can hold values that can not be represented as an int, i.e. INT_MAX+1 .. UINT_MAX.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top