关于这三者的区别stackoverrflow里有一个答案是这样说的:

3.9.1 Fundamental types [basic.fundamental]

1 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. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (basic.types); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. For unsigned 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.

首先要明确的是,他们是三种不同的类型:

  在VS2013的STL库实现里面,有这样的代码:

 

inline void _Fill(char *_First, char *_Last, char _Val)
{ // copy char _Val through [_First, _Last)
_CSTD memset(_First, _Val, _Last - _First);
} inline void _Fill(signed char *_First, signed char *_Last, signed char _Val)
{ // copy signed char _Val through [_First, _Last)
_CSTD memset(_First, _Val, _Last - _First);
} inline void _Fill(unsigned char *_First, unsigned char *_Last, unsigned char _Val)
{ // copy unsigned char _Val through [_First, _Last)
_CSTD memset(_First, _Val, _Last - _First);
}

可以看到,写了三个重载函数,分别针对这三种类型。

其次,他们占用的内存是一样的:

  在C/C++里面用sizeof运算符的时候,得到的大小是以一个char为基本单位的,也就是说无论采用哪种实现,对这三种类型得到的结果都是1.

最后:

  对于字符串表示,毫无疑问应该用char,标准并不限定这个char是有符号还是无符号的。至于signed char、unsigned char只是为了得到一个相应大小的整数型数据而已!

char, signed char, and unsigned char in C++的更多相关文章

  1. C语言char*字符串数组和unsigned char[]数组的相互转换

    #include <iostream> #include <string> using namespace std; void convertUnCharToStr(char* ...

  2. signed char、unsigned char

    什么是无符号char类型?与常见的char类型有何不同? 在c++中有三种不同的字符类型:char,signed char,unsigned char.如果要应用与文本字符,就使用不加限制的char类 ...

  3. C 中 char、signed char 和 unsigned char 的区别

    C 中 char.signed char 和 unsigned char 的区别 来源:http://bbs.chinaunix.net/thread-889260-1-1.html 参考:https ...

  4. 图像处理中像素点的问题:unsigned char 和 char

    以前在做图像处理的时候,一直不太在意这个问题,对图像每个像素点的灰度值,总是认为char也可,unsigned char也可.尽管它们都是8位,但是表示的数的范围却不相同:char: -128~127 ...

  5. 关于 char 和 unsigned char 的区别

    首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符?   char buf[16] = {0}; unsigned char ubuf[16] = { 0 };   上面两个定义的区别是: ...

  6. QString unsigned char* 的转换

    QString -> unsigned char* : QString str = "ABCD";  int length = str.length(); unsigned ...

  7. Integral Promotions整数提升和符号扩展(char,unsigned char,signed char)

    以下来自msdn: Objects of an integral type can be converted to another wider integral type (that is, a ty ...

  8. char、signed char 和 unsigned char 的区别

    ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.而不是像short.int一样只有两种(int默认就是signed int). 三者都占1个字节( ...

  9. char , unsigned char 和 signed char 区别

    ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.char相当于signed char或者unsigned char,但是这取决于编译器!这三种字符 ...

随机推荐

  1. 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator

    [Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...

  2. Azure平台 对Twitter 推文关键字进行实时大数据分析

    Learn how to do real-time sentiment analysis of big data using HBase in an HDInsight (Hadoop) cluste ...

  3. CentOS 7 /RHEL 7: How To Change The System Locale

    The system localeare used to control the language setting of system services and the UI before the u ...

  4. HDU----(4291)A Short problem(快速矩阵幂)

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. 使用 HTML5 canvas 绘制精美的图形

    HTML5 是一个新兴标准,它正在以越来越快的速度替代久经考验的 HTML4.HTML5 是一个 W3C “工作草案” — 意味着它仍然处于开发阶段 — 它包含丰富的元素和属性,它们都支持现行的 HT ...

  6. Java 8的五大开发技巧

    转载:http://geek.csdn.net/news/detail/94219 在Java 9发布之前,我们来分享一些Java 8开发技巧,本文翻译自JetBrains高级开发主管Trisha G ...

  7. Jquery 表格操作,记录分页情况下,每一页中被用户勾选的信息

    如下图,一个分页列表,用户可以随意勾选一条或多条信息,然后进行某种操作,如“提交”.但是有个问题:如果勾选了一条信息之后,点[下一页],那么上一页 勾选的条目被刷新掉了. 问题:如果用户需要在第1页, ...

  8. outlook 用宏发邮件

    经常发面试邮件,通常只是修改一下收件人邮箱地址,和收件人姓名,其他全部一致,有木有发现每次都用用outlook写邮件很麻烦? 使用宏发邮件,就会不麻烦了,直接修改下称呼,修改下收件人地址,按下F5,就 ...

  9. myeclipse中java文件中文注释乱码问题

    在myeclipse中,有时打开java文件会发现中文注释全为乱码了,这个问题主要是因为编码的问题没有设置好,一个重要的原则就是保证所有的编码一致才不会发生乱码 出现乱码,需要知道三个地方的编码格式: ...

  10. linux 安装软件,卸载软件 等的几种方式

    安装软件的步骤和基本原则: 1. rpm安装: 定义: rpm是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装.查询.卸载.升级等工作.但是rpm软件包之间的依赖性问题往往会很 ...