Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'u' mean after a number?

Tags:

c

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

#define NAME_DEFINE 1u  
like image 418
Lodhart Avatar asked Jan 27 '12 07:01

Lodhart


People also ask

What does %u do in C?

The %u format specifier is implemented for fetching values from the address of a variable having an unsigned decimal integer stored in the memory. It is used within the printf() function for printing the unsigned integer variable.

What does 1U mean in C?

1U is unsigned. It can carry values twice as big, but without negative values. Depending on the environment, when using U, i can be a maximum of either 31 or 15, without causing an overflow. Without using U, i can be a maximum of 30 or 14. 31, 30 are for 32 bit int.

What does the U mean at the end of a number in C?

It means it's an unsigned int constant. It's a way of telling the compiler to use a specific type for a constant where it wouldn't otherwise know the type.

What is 10u in C?

-10u is a "unary minus" operator applied to a constant 10u of type unsigned int . The result is value of -10 represented in the domain of unsigned int type. According to the rules of unsigned arithmetic, the final result will be equal to UINT_MAX + 1 - 10 and will have type unsigned int .


2 Answers

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.

like image 176
Lundin Avatar answered Sep 16 '22 18:09

Lundin


It is a way to define unsigned literal integer constants.

like image 36
Basile Starynkevitch Avatar answered Sep 16 '22 18:09

Basile Starynkevitch