I am pretty much sure this question has been answered, though I didn't manage to find it. I know the rules for the type conversions like this. Even if we assign 1
(which is by default of a type signed int
) to unsigned int
variable, the variable of type unsigned int
will have the value if 1
in either case. In other words, why would I want to put U
suffix, unless to avoid type conversion (if I intend to assign that value mostly to unsigned int
s)?
-
1What will be the result of `0xFFFFFFFF >> 1` on a 32-bit int compiler? What will be `0xFFFFFFFFU >> 1` – Eugene Sh. Oct 25 '16 at 14:27
-
2@EugeneSh. `0xFFFFFFFF` is an `unsigned` on a 32-bit int/unsigned compiler. `0xFFFFFFFFU` is also an unsigned value. What difference do you see? – chux - Reinstate Monica Oct 25 '16 at 14:31
-
Is it due to the error prone, when typing in HEX format? – Hairi Oct 25 '16 at 14:35
-
1@chux You are right. Lets make it `-1 >> 1` versus `-1U >> 1` . https://ideone.com/LzkKnK – Eugene Sh. Oct 25 '16 at 14:36
-
`0x7FFFFFFF << 1` shifts a signed type, likely into the sign position --> issues. `0x7FFFFFFFu << 1` --> no problem. – chux - Reinstate Monica Oct 25 '16 at 14:37
-
I think the genesis of the `u` suffix is more subtle. May be it has something to do with the processor's work optimization. For example: let suppose `int` is 8 bit long. Then this `unsigned int i = 255` would result in promoting `255` to an signed integer that can represent the number `255`, BUT the processor is not that good in calculus with numbers bigger than 8-bit (which is 255 - int16_t may be). Therefore if we put `u` behind the number like this: `255U` integer promotion would happen and processor is very happy when working with 8-bit numbers. – Hairi Oct 26 '16 at 08:34
-
The upper comment is just a consideration that I do not persist to be true, but I hope it will help to understand my question. – Hairi Oct 26 '16 at 08:36
-
@Hari If `int` is 8-bit, 255 is _not_ an `int`. It would be a wider type, like `long`. then `unsigned int i = 255` is assigning a `long` to `unsigned`. Assignment of out of range values to an `unsigned` is well-defined. If code was `unsigned int i = 255u`, that would be assigning an `unsigned` to `unsigned`. No promotion occurs. In this example, the net result is not different. But if code was `#define A -1u unsigned b = A << 1;` the results can differ. – chux - Reinstate Monica Oct 26 '16 at 14:19
2 Answers
Literal value suffixes are most important when you need to precisely control the type. For example 4 billion fits in an unsigned 32-bit int but not a signed one. So if you do this, your compiler may complain:
printf("%u", 4000000000);
warning: format specifies type 'unsigned int' but the argument has type 'long'
Also one may use the float suffix f
to ensure a value is used that way in arithmetic, such as 1f / x
(which could also be written 1. / x
or 1.0 / x
). This is important if x
might be an integral type but the result is meant to be floating point.

- 239,568
- 38
- 324
- 436
-
3Literals don't need suffixes "to fit". The type of the literal is always suitable to hold that value, if such a type exists. – Kerrek SB Oct 25 '16 at 14:37
-
@KerrekSB: That's true; I reworded part of my answer and gave a concrete example where it matters. – John Zwinck Oct 25 '16 at 14:41
-
1It's still not about the value being "out of range in the desired type". If that's the case, then you're just out of luck. On the contrary, it's about designating a different signedness or minimum integer rank (or both) than would be selected by default. – John Bollinger Oct 25 '16 at 14:52
-
1@KerrekSB Mostly, but not fully agree with [Literals don't need suffixes "to fit"](http://stackoverflow.com/questions/40242582/why-do-we-put-suffixes-following-numeric-literals/40243626?noredirect=1#comment67747752_40242705) That applies for `l` and `ll`, but not `u`. Consider [integer constant greater than `LLONG_MAX`](http://stackoverflow.com/a/40265895/2410359) – chux - Reinstate Monica Oct 26 '16 at 15:15
-
An integer constant does not need a suffix to exist with a given value (aside from values representable as some unsigned in decimal but not signed). The trick is what type is that constant and how is that used.
Suppress warning as the integer decimal constant cannot be presented as a signed long long
.
// pow(2,64) - 1
unsigned long long x1 = 18446744073709551615; // warning
unsigned long long x2 = 18446744073709551615u;// no warning
Consider @Eugene Sh. example
-1 >> 1 // Rigth shifting negative values is implementation defined behavior
// versus
-1U >> 1 // Well defined. -1U has the positive value of `UINT_MAX`
Sometime simple constants like 1u
are used for gently type conversion
// The product is the wider of the type of `x` or `unsigned`
x*1u
@John Zwinck provides a good printf()
example.
The suffixes u
and U
insure the type is some unsigned integer like unsigned
or wider.
The is no suffix to insure the type is signed. Use decimal constants.
The suffixes l
and L
insure the type is at least long/unsigned long
without changing it sign-ness.
The suffixes ll
and LL
insure the type is at least long long/unsigned long long
without changing it sign-ness.
The is no suffix to insure the type is narrower than int/unsigned
.
There is no standard suffix to insure the type is intmax_t/uintmax_t
.

- 1
- 1

- 143,097
- 13
- 135
- 256
-
Beside the warnings (whose motivation I don't understand), I can't find reasonable explanation for using suffixes of type 'U' or 'u'. Being signed or unsigned integer literal does not affect the bit representation of the result. Once it is assigned to a variable, integer promotion be performed if the variable is qualified as unsigned and has rank greater than or equal. – Hairi Oct 26 '16 at 06:45
-
I very much appreciate your efforts, although (may be because I can't put the question right) I don't understand how would it help me to put this `u/U` at the end of the integer literal. Why on the earth, would someone write something like this: `-1U' ? It doesn't make sense to me. – Hairi Oct 26 '16 at 08:32
-
@Hairi "Being signed or unsigned integer literal does not affect the bit representation of the result." This is not clear and certainly not supported by the C standard. Provide examples of what you mean. – chux - Reinstate Monica Oct 26 '16 at 14:10
-
@Hairi "Why ...write something like this: `-1U' ? It doesn't make sense to me." vs what? Clearly state a given usage for a clear answer. – chux - Reinstate Monica Oct 26 '16 at 14:11