2

After reading quite a couple of questions on integer promotions, it seems to be the common understanding that integer promotions or only applied to small integer types, such as short int or char.

However, I'm wondering why an unsigned int variable of i. e. value 15 shouldn't be promoted to an int as well. After all, it's conversion rank is equal to the rank of int and unsigned int, as requested by statement (1) in the citation below.

As an int can represent the value 15 without any problems (on all plattforms I know of), it should get converted to an int.

Integer promotions

The following may be used in an expression wherever an int or unsigned int may be used:

  1. An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned int.

  2. A bit-field of type _Bool, int, signed int, or unsigned int.

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.

Multisync
  • 767
  • 6
  • 25
  • With `unsigned a=1; long long b=2; b = a+b;`, `a` is promoted to `long long` before the addition. – chux - Reinstate Monica Sep 02 '14 at 20:10
  • It is not clear to me what you are asking, are you asking for a rationale or are you asking why the wording does not allow that? – Shafik Yaghmour Sep 02 '14 at 20:12
  • May be related depending on what your question really is: [Why must a short be converted to an int before arithmetic operations in C and C++?](http://stackoverflow.com/questions/24371868/why-must-a-short-be-converted-to-an-int-before-arithmetic-operations-in-c-and-c) – Shafik Yaghmour Sep 02 '14 at 20:15
  • May be this will help you http://stackoverflow.com/questions/13600991/why-is-a-negative-int-greater-than-unsigned-int – Vikas Verma Sep 02 '14 at 20:17
  • With `unsigned short a; unsigned short b;`, `a+b` infamously morphs into an `int`. – Spencer Apr 07 '22 at 16:32

2 Answers2

8

However, I'm wondering why an unsigned int variable of i. e. value 15 shouldn't be promoted to an int as well. [...] an int can represent the value 15 without any problems

There are two problems with this statement:

  • Integer promotions does not mean "promotion to an int"; it means "promotion to either an int or unsigned int". Therefore, "promoting" an unsigned int does not make sense: it is already promoted.
  • Integer promotion rules do not take into consideration the current value of an expression. The rules are specifically written in a way to talk about all values of a type. Hence, the fact that an int is capable of representing the value 15 is irrelevant, because int is not capable of representing all values of unsigned int.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I'm not yet sure if I should mark this as the accepted answer. While the second clause is definately true on all platforms I know of, the first clause is not yet satisfactory to me. I'm with you that "promoting" an `unsigned int` does not make sense. Yet the reason I posted this question was: Why is a promotion of `unsigned int` to `int` legaly allowed, altough it doesn't make sense? After all, the standard says: _whose integer conversion rank is less than **or equal** to the rank of int and unsigned int_! I hope you got my point. I'm no native speaker, and this is quite a complicated topic. – Multisync Sep 04 '14 at 19:18
  • @Multisync "Why is a promotion of `unsigned int` to `int` legally allowed, although it doesn't make sense?" It is not legally allowed: the rule says that "If an `int` can represent **all values** of the original type, the value is converted to an `int`". Since `int` can represent *some*, but not *all*, values of `unsigned int`, this promotion is not considered, in spite of the types having identical conversion rank (in fact, all corresponding `X` (signed) and `unsigned X` types always have identical conversion rank). – Sergey Kalinichenko Sep 04 '14 at 19:40
  • Hmm.. okay. Still I don't understand why they put the "_or equal_" there. If my understanding of the rank schemata is correct, then there can not be a type with a rank equal to that of `unsigned int` or `int`, so putting that in there doesn't make any sense. – Multisync Sep 04 '14 at 20:28
3

Usually, it's impossible to say at compile-time what values will variables hold when promotion will actually occur.

Investigating value of variable to choose appropriate type at runtime introduces too much overhead, as well as simply impossible. So, only things compiler has are types.

I think main reason to prefer unsigned types over signed is that unsigned integer overflow is defined, while overflow of signed integer is undefined behavior.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35