Integer promotion (MISRA C:2012 Rule 7.2)

不想你离开。 提交于 2020-01-04 05:32:28


MISRA enforces the use of the U suffix for unsigned integer constants

uint32_t the_answer = 0x42U;

I feel the U is a bit boilerplate because the line is very understandable without it.

So I am wondering how much this rule is important and if unsigned int x = 1 is truely a bad example of implicit integer promotion.


You are correct, the U in this specific example is superfluous as per an exception to Rule 10.3:

"A non-negative integer constant expression of essentially signed type may be assigned to an object of essentially unsigned type if its value can be represented in that type."

Therefore, you are fine to assign a signed integer less than 7FFF FFFF to this unsigned 32-bit object.