Search code examples
c++ctypescastinginteger-promotion

When does a type get promoted to an unsigned int?


This article says:

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

All values of signed/unsigned char and signed/unsigned short can be represented as an int, so when does a type gets promoted to an unsigned int?


Solution

  • A short cannot be longer than an int but on some platforms it may be the same size. The same goes for int and long. This means that if the "smaller" one is unsigned, the "larger" one must be too.