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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With