Aviv Cohn
Aviv Cohn

Reputation: 17243

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?

Upvotes: 9

Views: 746

Answers (1)

Bathsheba
Bathsheba

Reputation: 234785

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

Upvotes: 5

Related Questions