48

Can you tell me what exactly does the u after a number, for example:

#define NAME_DEFINE 1u 
Neuron
  • 5,141
  • 5
  • 38
  • 59
Lodhart
  • 635
  • 2
  • 9
  • 13

5 Answers5

72

Integer literals like 1 in C code are always of the type int. int is the same thing as signed int. One adds u or U (equivalent) to the literal to ensure it is unsigned int, to prevent various unexpected bugs and strange behavior.

One example of such a bug:

On a 16-bit machine where int is 16 bits, this expression will result in a negative value:

long x = 30000 + 30000;

Both 30000 literals are int, and since both operands are int, the result will be int. A 16-bit signed int can only contain values up to 32767, so it will overflow. x will get a strange, negative value because of this, rather than 60000 as expected.

The code

long x = 30000u + 30000u;

will however behave as expected.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 6
    `this expression will result in a negative value`. Well or demons will fly out of your nose as integer overflows are undefined behavior. – ouah Jan 27 '12 at 09:01
  • 4
    @ouah In theory yes. In the real world, all compilers I have ever seen handle integer overflows in the same manner. Anyway, it is a bug regardless of the result. – Lundin Jan 27 '12 at 10:10
  • 9
    the fact that integer overflow is undefined is not only theoritical. Even in the real world, compilers take advantage of integers overflow being undefined behavior to perform optimizations. `gcc` for example has at least 20 cases where it doesn't consider integer overflow to wrap so it can perform optimization. A simple example is an expression like `a - 8 < 42`, if `a` is a signed type `gcc` could reduce the expression to `a < 50`. – ouah Jan 27 '12 at 10:28
  • Replying to @ouah's comment: unsigned overflow _is_ defined, signed overflow is not. See: [this SO question](http://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is) – svec Apr 05 '17 at 19:59
  • @svec in C terminology integer overflow *is* undefined behavior because only signed integer can overflow. See C11, 3.4.3p3 "An example of undefined behavior is the behavior on integer overflow" and 6.2.5p7 "A computation involving unsigned operands can never overflow, [...]" – ouah Apr 05 '17 at 21:19
  • @Lundin I am getting correct result for this: signed long long var3 = 2147483648+2; In this case both operands in RHS are ints and result is int and must overflow right? But result is 2147483650. – Rajesh Jan 16 '18 at 16:31
  • @Rajesh Integer literals have different types depending on the size of the number. But instead of hijacking a 5 year old question's comment field, you should ask a new one by clicking "ask a question". – Lundin Jan 17 '18 at 07:26
  • `long x = 30000u + 30000u;` works, but `long x = 40000u + 40000u;` might not. A safer approach is `long x = 30000L + 30000L;` – chqrlie May 17 '21 at 20:46
18

It is a way to define unsigned literal integer constants.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
4

It is a way of telling the compiler that the constant 1 is meant to be used as an unsigned integer. Some compilers assume that any number without a suffix like 'u' is of int type. To avoid this confusion, it is recommended to use a suffix like 'u' when using a constant as an unsigned integer. Other similar suffixes also exist. For example, for float 'f' is used.

Mocha
  • 126
  • 5
  • Not "some compilers". All compilers. – Lundin Jan 27 '12 at 07:50
  • I did not want to generalize, since I personally have used only a couple of compilers. – Mocha Jan 27 '12 at 08:06
  • My point is that the C standard enforces the compiler to treat an integer literal without 'u' as signed int. – Lundin Jan 27 '12 at 08:45
  • 3
    @Lundin Not exactly correct, it can also be a long or long long. Without suffix, an integer literal's type is the first of `int`, `long` and `long long` which can hold the value (if any). – Daniel Fischer Jan 27 '12 at 13:00
  • @DanielFischer: That is true. But it will always be of signed type unless you write the 'u'. – Lundin Jan 27 '12 at 15:16
2

it means "unsigned int", basically it functions like a cast to make sure that numeric constants are converted to the appropriate type at compile-time.

  • 1
    Yes, but *H2CO3* said "it *functions* like a cast", he didn't say it *is* a cast! – Basile Starynkevitch Jan 27 '12 at 07:09
  • I mean, without the "u" it would be signed as that's the default for integer constants. So tge u is a notice to the compiler to take it as unsigned. I *know* that it's not a cast, it was only a sample for better understanding. –  Jan 27 '12 at 07:10
1

A decimal literal in the code (rules for octal and hexadecimal literals are different, see https://en.cppreference.com/w/c/language/integer_constant) has one of the types int, long or long long. From these, the compiler has to choose the smallest type that is large enough to hold the value. Note that the types char, signed char and short are not considered. For example:

0 // this is a zero of type int
32767 // type int
32768 // could be int or long: On systems with 16 bit integers
      // the type will be long, because the value does not fit in an int there.

If you add a u suffix to such a number (a capital U will also do), the compiler will instead have to choose the smallest type from unsigned int, unsigned long and unsigned long long. For example:

0u // a zero of type unsigned int
32768u // type unsigned int: always fits into an unsigned int
100000u // unsigned int or unsigned long

The last example can be used to show the difference to a cast:

100000u // always 100000, but may be unsigned int or unsigned long
(unsigned int)100000 // always unsigned int, but not always 100000
                     // (e.g. if int has only 16 bit)

On a side note: There are situations, where adding a u suffix is the right thing to ensure correctness of computations, as Lundin's answer demonstrates. However, there are also coding guidelines that strictly forbid mixing of signed and unsigned types, even to the extent that the following statement

unsigned int x = 0;

is classified as non-conforming and has to be written as

unsigned int x = 0u;

This can lead to a situation where developers that deal a lot with unsigned values develop the habit of adding u suffixes to literals everywhere. But, be aware that changing signedness can lead to different behavior in various contexts, for example:

(x > 0)

can (depending on the type of x) mean something different than

(x > 0u)

Luckily, the compiler / code checker will typically warn you about suspicious cases. Nevertheless, adding a u suffix should be done with consideration.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47