• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > 详谈signed 关键字

详谈signed 关键字

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了鬼怪详谈祥云寺,详谈,鬼话详谈祥云寺,凯迪拉克ct6功能详谈,详谈细论二十八脉等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

我们都知道且经常用到 unsigned 关键字,但有没有想过,与此对应的 signed 关键字有啥用?

int i = 0;
signed int i = 0;
</div>

这俩有区别吗?没区别,看起来,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.
</div>

标准规定的很清楚,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.
</div>

看来,bit-fields (位域) 也存在同样的问题。(位域的概念可能也有点偏,经常写比较底层的接口或协议童鞋应该熟悉,可参考这里)

结论

在 C/C++ 中,signed 关键字绝大多数情况下都是累赘,但对于上述所言的两种情况,即在 char 与 bit-fields 的使用过程中,还是有比较隐晦的作用的。

给自己提个醒,总是好的。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 详谈C++的内存泄漏问题
  • 详谈c++11 final与override说明符
  • 详谈C++何时需要定义赋值/复制构造函数
  • 详谈signed 关键字

相关文章

  • 2017-05-28C语言实现支持动态拓展和销毁的线程池
  • 2017-05-28探讨C++中不能声明为虚函数的有哪些函数
  • 2017-05-28结合C++11新特性来学习C++中lambda表达式的用法
  • 2017-05-28c语言获取文件大小的示例
  • 2022-04-30C语言位运算(按位与运算、或运算、异或运算、左移运算、右移运算)
  • 2017-05-28C++ 简单实现MFC ListControl 点击列头排序
  • 2017-05-28浅析stl序列容器(map和set)的仿函数排序
  • 2017-05-28C++ 中placement new 操作符使用方法
  • 2017-05-28马尔可夫链算法(markov算法)的awk、C++、C语言实现代码
  • 2017-05-28Linux系统中C语言编程创建函数fork()执行解析

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++程序中使用Windows系统Native Wifi API的基本教程
    • 理解数据结构
    • C++日志记录类实例解析
    • C语言中system()函数的用法总结
    • C++未定义行为(undefined behavior)
    • C语言中的fscanf()函数与vfscanf()函数使用
    • 在VC中隐藏控制台程序窗口的实现代码
    • Win32应用程序(SDK)设计原理详解
    • C/C++函数调用的几种方式总结
    • CFileDialog设置多选的问题解决

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有