您的位置:首页 > 其它

char 型数据本质上是没有算术和逻辑运算的

2012-09-13 14:35 267 查看
------------------------------------------------------------------------------------------------------------------------------

"A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer maybe used. If an
int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion."

------------------------------------------------------------------------------------------------------------------------------------------------


(戳)MISRA C: Motor
Industry Software Reliability Association

整数提升(Integral promotion)转换

整数提升描述了一个过程,借此过程数值操作总是在int 或long(signed 或unsigned)整型操作数上进行。其他整型操作数(char、short、bit-field 和enum)在数值操作前总是先转化为int 或unsigned int 类型。这些类型称为small integer 类型。整数提升的规则命令,在大多数数值操作中,如果 int 类型能够代表原来类型的所有值,那么small integer 类型的操作数要被转化为int 类型;否则就被转化为unsigned int。

注意,整数提升
- 仅仅应用在 small integer 类型上
- 应用在一元、二元和三元操作数上
- 不能用在逻辑操作符(&&、||、!)的操作数上
- 用在 switch 语句的控制表达式上

[规则 6.1] : 单纯的 char 类型应该只用做存储和使用字符值。

[规则 6.2] : signed char 和 unsigned char 类型应该只用做存储和使用数字值。

[规则10.5] : 如果位运算符 ~ 和 << 应用在基本类型为unsigned char 或unsigned short 的操作数,结果应该立即强 制转换为操作数的基本类型。

==================================================================================== 更多参考文档(戳) MISRA
C:2004

====================================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐