Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have to append `u` suffix to unsigned integers?

I know the u suffix means 'unsigned'. But is in necessary in the following code?

uint32_t hash = 2166136261u;

Is it a matter or convention? Or does it have any technical significance in this case? The value should be converted to unsigned anyway because uint32_t is unsigned.

When should I and when should I not use the u suffix for unsigned integer values?

like image 295
Aviv Cohn Avatar asked Apr 12 '19 16:04

Aviv Cohn


1 Answers

No it is not necessary. Things get interesting at 2147483648 and your number is greater than this.

Note that formally 2166136261 is a long or a long long type if int has 32 bits or fewer. But either are convertible to a uint32_t in a well-defined way.

As a final point: the equivalent hex 0x811C9DC5 is an unsigned type if int has 32 bits or more. Oh joy!

Reference: https://en.cppreference.com/w/c/language/integer_constant

like image 188
Bathsheba Avatar answered Oct 14 '22 19:10

Bathsheba