72

What does the postfix (or suffix) U mean for the following values?

0U
100U
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
lovespring
  • 19,051
  • 42
  • 103
  • 153

2 Answers2

81

It stands for unsigned.

When you declare a constant, you can also specify its type. Another common example is L, which stands for long. (and you have to put it twice to specify a 64-bit constant).

Example: 1ULL.

It helps in avoiding explicit casts.

sixter
  • 163
  • 1
  • 3
  • 11
ruslik
  • 14,714
  • 1
  • 39
  • 40
  • 11
    There are also cases where it's necessary. For instance, integral constants are interpreted as integers by the compiler, so a constant like `0xffffffffffffffff` will lose its high 32 bits without the `ll` suffix. – zneak Dec 14 '11 at 18:48
  • 2
    @zneak actually, it won't. Integral literals have the type that can hold the value (if such a type exists). See [The type of the literal](https://en.cppreference.com/w/cpp/language/integer_literal#The_type_of_the_literal). Thus, the type of your example literal is `long unsigned int` on GCC 8.4.0 x86_64. – Ruslan Sep 04 '20 at 12:44
52

Integer constants in C and C++ can optionally have several suffixes:

  • 123u - the value 123 is an unsigned int
  • 123l - (that's a lowercase L) 123 is a signed long
  • 123L - ditto
  • 123uL - unsigned long
  • 123LL - a signed long long, a 64 bit or 128 bit value (depending on the environment)
  • 123uLL - unsigned long long

You can read more here: https://en.cppreference.com/w/cpp/language/integer_literal

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • [cppreference](http://en.cppreference.com/w/cpp/language/integer_literal) says for "12345...u" that "the type is unsigned long long even without a long long suffix". Quite confusing, since your answer says 123u is unsigned int. Who is right, or where is the misunderstanding? – Ela782 Mar 23 '18 at 22:47
  • @Ela782: I looked at cpp reference, but couldn't find anything which says "u" implies long long. Did I misunderstand? – wallyk Mar 23 '18 at 23:59
  • If you click my link in the comment above, near the bottom of that page, as a comment in the code example, it says: `<< 12345678901234567890u << '\n'; // the type is unsigned long long even without a long long suffix`. Did you find that? It's certainly possible that I misunderstand that sentence. – Ela782 Mar 24 '18 at 10:42
  • @Ela782: Oh, I see. That comment presumes it is compiled for a machine which *has* `long long` and since the constant exceeds the range for `long` it is already a `long long`. – wallyk Mar 26 '18 at 17:59
  • 1
    Ooh I get it now, I see! Thank you! You mean "since it exceeds the range for (unsigned) `int`" though, because `u` would make it an (unsigned) `int` - but because the number is so large, it'll make an (unsigned) `long long`. Correct? – Ela782 Mar 27 '18 at 19:45
  • 2
    @Ela782: Yep. I am not sure off the top of my head, but without the `u` it could well be a signed long long. (checked) Nope, it overflows a 64-bit signed int. The `u` is needed. If the architecture it were compiled for had 128-bit `int`s one could choose either signed or unsigned. – wallyk Mar 28 '18 at 01:38
  • Ok cool, thank you very much for this explanation! Makes it very clear now :-) – Ela782 Mar 29 '18 at 00:08