详谈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语言高效实现向量循环移位

    C语言高效实现向量循环移位

    这篇文章主要为大家详细介绍了C语言高效实现向量循环移位,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • QT中线程池QThreadPool类概念和使用方法详解

    QT中线程池QThreadPool类概念和使用方法详解

    这篇文章主要为大家介绍了QT中线程池QThreadPool类概念和使用方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • 浅析C++ atomic 和 memory ordering

    浅析C++ atomic 和 memory ordering

    这篇文章主要介绍了C++ atomic 和 memory ordering的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 关于C语言strlen与sizeof区别详情

    关于C语言strlen与sizeof区别详情

    对于 strlen 和 sizeof,相信不少程序员会混淆其功能。虽然从表面上看它们都可以求字符串的长度,但二者却存在着许多不同之处及本质区别,今天得这篇文章我们就来学习C语言strlen与sizeof区别的相关资料,需要的朋友可以参考一下
    2021-10-10
  • C语言数据结构不挂科指南之队列详解

    C语言数据结构不挂科指南之队列详解

    这篇博客主要介绍一下队列的概念,并且采用 C 语言,编写两种存储实现方式:顺序存储和链式存储,当然还有常规的队列基本操作的实现算法
    2022-09-09
  • C语言三子棋一步步实现详程

    C语言三子棋一步步实现详程

    三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了,想用c语言做出这个游戏,事实上也是比较简单的,下面通过c语言进行对五子棋的分析
    2022-02-02
  • 实现去除c语言注释的小工具

    实现去除c语言注释的小工具

    这篇文章主要介绍了实现去除c语言注释的小工具,说是C语言,但其实所有C语系的都可以,比如Java,需要的朋友可以参考下
    2014-02-02
  • 基于C语言实现简单的扫雷游戏

    基于C语言实现简单的扫雷游戏

    这篇文章主要为大家详细介绍了基于C语言实现简单的扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • C++使用fdk-aac实现将音频PCM编码成aac

    C++使用fdk-aac实现将音频PCM编码成aac

    mp4的音频流通常是aac编码,我们做音视频采集的时候就需要将,采集的音频PCM编码成aac,本文就来为大家介绍一下C++如何使用fdk-aac实现将音频PCM编码成aac吧
    2023-11-11
  • 基于Qt实现电子木鱼小游戏

    基于Qt实现电子木鱼小游戏

    今年最火爆的解压小游戏电子木鱼,现在许多软件都上架了这个小程序。我在网上看了一下基本上都是用py和Java写的,所以我用QT重新写了一下,作为小白练手项目非常适合,快跟随小编一起学习一下吧
    2023-01-01

最新评论