Roland
Roland

Reputation: 7875

Integer promotions in C will downgrade `long int`?

From the C standard

6.3.1.1

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.

So this means that if I use a long int in an expression it will be downgraded to an unsigned int?

Upvotes: 3

Views: 246

Answers (2)

Michael Burr
Michael Burr

Reputation: 340506

The bit you quoted is restricted by the text above it:

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

  • An object or expression with an integer type whose integer conversion rank is less than the rank of int and unsigned int.
  • A bit-field of type _Bool, int, signed int,or unsigned int.

If an int can represent all values of the original type, 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.

In other words, long int doesn't get promoted to int or unsigned int.

Upvotes: 3

1''
1''

Reputation: 27125

I think "original type" refers to "[...] an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int", as defined earlier in section 6.3.1.1.2. But, nice try :)

Upvotes: 1

Related Questions