3

I'm sure this has a very simple answer, but it is a difficult question to search for:

What is the significance (necessity?) of adding a 'u' after an integer in C.

Example:

unsigned char Buffer1[2048];
unsigned char Buffer2[2048u];

Would anyone be kind enough to explain the importance of the 'u'?

I appreciate the help,

Edit: In addition to 'what' the 'u' stands for, I would like a better understanding of 'why' you would need to use it.

Again, thank you for your input,

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Nanomurf
  • 709
  • 3
  • 12
  • 32
  • Do you have more context to this question? Is the code you are asking about exactly like you posted? It is not apparent from your example that it makes a difference, the standard requires that the size be an integer and greater than zero. – Shafik Yaghmour Dec 06 '13 at 15:43
  • Shafik - yes, here is my context: I have encountered this suffix for the first time in a function I am editing. I will be altering the buffer size through a parameter instead of a constant, and I am not clear why the 'u' was added in my case. I'd like to better understand 'why' one would need to use it. Thanks for your input, – Nanomurf Dec 06 '13 at 15:45
  • SBI - yes, that question is close to what I'm asking for. But I guess specifically for the C language, I am still not seeing when it would be necessary to use this casting... – Nanomurf Dec 06 '13 at 15:49
  • 1
    If the code looks like what you provided then I don't see any difference, be aware that using a variable for the array size will be using [variable length arrays](http://stackoverflow.com/questions/18521398/which-compiler-should-i-trust), so the code won't work in all compilers. – Shafik Yaghmour Dec 06 '13 at 16:00

2 Answers2

3

It makes the integer literal unsigned: 2048 is a (signed) int, 2048u is an unsigned int.

Likewise, l or L makes it a long instead of an int, and ul or UL makes it an unsigned long.

(Note: Please use L rather than l because the lower-case l can be easily confused with the digit 1).

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

What you write as 2048 is an integer literal of type int. By adding an u after it, you signify the type to be unsigned int, not merely int.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313