整数提升(MISRA C:2012 Rule 7.2) [英] Integer promotion (MISRA C:2012 Rule 7.2)

查看:64
本文介绍了整数提升(MISRA C:2012 Rule 7.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MISRA强制对无符号整数常量使用 U 后缀

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

uint32_t the_answer = 0x42U;

我觉得 U 有点像样板,因为没有它,这行很容易理解.

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

所以我想知道这个规则有多重要,以及 unsigned int x = 1 是否真的是隐式整数提升的错误示例.

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.

推荐答案

您是正确的,根据规则10.3的例外,此特定示例中的U是多余的:

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."

因此,可以为此无符号32位对象分配小于7FFF FFFF的有符号整数.

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

这篇关于整数提升(MISRA C:2012 Rule 7.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆