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.


回答1:


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.



来源:https://stackoverflow.com/questions/48703624/integer-promotion-misra-c2012-rule-7-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!