简体   繁体   English

整数提升(MISRA C:2012 Rule 7.2)

[英]Integer promotion (MISRA C:2012 Rule 7.2)

MISRA enforces the use of the U suffix for unsigned integer constants MISRA强制将U后缀用于无符号整数常量

uint32_t the_answer = 0x42U;

I feel the U is a bit boilerplate because the line is very understandable without it. 我觉得U有点像样板,因为没有它,这条线很容易理解。

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. 所以我想知道这个规则有多重要,如果unsigned int x = 1确实是隐式整数提升的不好例子。

You are correct, the U in this specific example is superfluous as per an exception to Rule 10.3: 没错,根据规则10.3的例外,此特定示例中的U是多余的:

"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. 因此,可以为该无符号的32位对象分配一个小于7FFF FFFF的有符号整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM