时间:2021-05-19
我们都知道且经常用到 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 的使用过程中,还是有比较隐晦的作用的。
给自己提个醒,总是好的。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在网站SEO优化中,可以将网站的关键字分为好几种,包括目标关键字,扩展关键字、辅助关键字、长尾关键字。平时我们所说的目标关键字就是网站的主要关键字,在用户搜索的
C语言的关键字共有32个,根据关键字的作用,可分其为数据类型关键字、控制语句关键字、存储类型关键字和其它关键字四类。关于数据类型的关键字有char:声明字符型变
1.关键字的种类 这儿我将关键字的种类分成a、b、c三种,分别是:主页关键字、频道栏目页关键字、内容页关键字。 a、对于主页关键字:大家必须掌握的便是主
文章标题撰写公式计算:文章标题=产品关键关键字商品属性关键字(能够使用价值关键字)1.特性关键字:产品分类、名字、型号规格、作用、特点的关键字。比如长袖连衣裙有
搜索关键字管理一、搜索关键字管理功能介绍:搜索关键字管理为对网站前台搜索关键字记录的管理。二、搜索关键字管理:(一)、登录后台,单击“栏目&rdqu