详谈signed 关键字

 更新时间:2015年01月20日 17:25:09   投稿:hebedich  
c++中关键字有几十个,其中类型修饰关键字有long, short, singed, unsigned。今天我们就来谈一下经常被大家忽视的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++ 中std::vector<T>的几种清除方式

    C++ 中std::vector<T>的几种清除方式

    std::vector<T> 可以通过多种方式清除(删除所有元素),本文主要介绍了C++ 中std::vector<T>的几种清除方式,具有一定的参考价值,感兴趣的可以了解一下
    2025-04-04
  • C++实现百度坐标(BD09)及GCJ02与WGS84之间的转换

    C++实现百度坐标(BD09)及GCJ02与WGS84之间的转换

    这篇文章主要为大家详细介绍了C++实现百度坐标(BD09)及GCJ02与WGS84之间的转换的方法,文中的示例代码讲解详细,希望对大家有所帮助
    2023-03-03
  • C语言编写实现学生管理系统

    C语言编写实现学生管理系统

    这篇文章主要为大家详细介绍了C语言编写实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • C++学习之命名空间详解

    C++学习之命名空间详解

    C++中,命名空间(namespace)是一个重要的概念。命名空间可以为函数、变量、类等定义作用域,避免与其他定义的名称发生冲突。下面我们就来了解一下如何使用C++命名空间,以及一些常见的操作吧
    2023-04-04
  • C++中Operator类型强制转换成员函数解析

    C++中Operator类型强制转换成员函数解析

    转换函数定义了由<类型说明符1>到<类型说明符2>之间的映射关系。可见,转换函数是用来将一种类型的数据转换成为另一种类型
    2013-09-09
  • C语言音乐播放器实例代码

    C语言音乐播放器实例代码

    文章给大家分享了用C语言音乐播放器的实例代码,对此有需要的朋友参考学习下。
    2018-07-07
  • QT实现贪吃蛇游戏代码详解

    QT实现贪吃蛇游戏代码详解

    本文主要为大家详细介绍了在QT中实现贪吃蛇游戏的详细教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C++ opencv ffmpeg图片序列化实现代码解析

    C++ opencv ffmpeg图片序列化实现代码解析

    这篇文章主要介绍了C++ opencv ffmpeg图片序列化实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • 基于Qt实现的自定义树结构容器

    基于Qt实现的自定义树结构容器

    在Qt框架中,尽管其提供了许多强大的容器类,但缺少一个通用的、灵活的树结构容器,所以本文将设计并实现一个可复用的自定义树结构容器,需要的可以参考下
    2024-12-12
  • 详解C++ new-handler机制

    详解C++ new-handler机制

    这篇文章主要介绍了C++ new-handler机制的相关资料,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下
    2020-11-11

最新评论