详谈signed 关键字
我们都知道且经常用到 unsigned 关键字,但有没有想过,与此对应的 signed 关键字有啥用?
int i = 0;
signed int i = 0;
这俩有区别吗?没区别,看起来,signed 完全是个累赘。
真的是这样吗?
我查阅了 C++11 的标准文档(草稿N3690),发现一些端倪:
3.9.1 Fundamental types
Objects declared as characters(char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types, collectively called narrow character types. A char,a signed char,and an unsigned char occupy the same amount of storage and have the same alignment requirements(3.11); that is,they have the same object representation. For narrow character types, all bits of the object representation participate in the value representation. For unsigned narrow character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.
标准规定的很清楚,char, signed char 和 unsigned char 是三种不同的类型。 char 会根据具体实现场景,而决定到底是 signed 还是 unsigned.
再看看 C11 的标准文档(ISO/IEC 9899:201x)呢?
6.7.2 Type specifiers
Each of the comma-separated multisets designates the same type, except that for bit-fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.
看来,bit-fields (位域) 也存在同样的问题。(位域的概念可能也有点偏,经常写比较底层的接口或协议童鞋应该熟悉,可参考这里)
结论
在 C/C++ 中,signed 关键字绝大多数情况下都是累赘,但对于上述所言的两种情况,即在 char 与 bit-fields 的使用过程中,还是有比较隐晦的作用的。
给自己提个醒,总是好的。
上一篇:C语言函数语法详解
栏 目:C语言
下一篇:C语言实现求定积分的方法
本文标题:详谈signed 关键字
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3161.html
您可能感兴趣的文章
- 01-10深入理解C++中常见的关键字含义
- 01-10浅谈C/C++中的static与extern关键字的使用详解
- 01-10探讨C语言中关键字volatile的含义
- 01-10C++关键字typename的深入理解
- 01-10C语言关键字auto与register的深入理解
- 01-10求32位机器上unsigned int的最大值及int的最大值的解决方法
- 01-10C语言关键字大全(共32个)
- 01-10深入解析unsigned int 和 int
- 01-10关于C/C++中static关键字的作用总结
- 01-10C++ Explicit关键字详细解析
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 08-05dedecms(织梦)副栏目数量限制代码修改