在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别

首先在内存中,char与unsigned char没有什么不同,都是一个字节,唯一的区别是,char的最高位为符号位,因此char能表示-128~127, unsigned char没有符号位,因此能表示0~255,这个好理解,8个bit,最多256种情况,因此无论如何都能表示256个数字。

在实际使用过程种有什么区别呢?

主要是符号位,但是在普通的赋值,读写文件和网络字节流都没什么区别,反正就是一个字节,不管最高位是什么,最终的读取结果都一样,只是你怎么理解最高位而已,在屏幕上面的显示可能不一样。

但是我们却发现在表示byte时,都用unsigned char,这是为什么呢?

首先我们通常意义上理解,byte没有什么符号位之说,更重要的是如果将byte的值赋给int,long等数据类型时,系统会做一些额外的工作。

如果是char,那么系统认为最高位是符号位,而int可能是16或者32位,那么会对最高位进行扩展(注意,赋给unsigned int也会扩展)

而如果是unsigned char,那么不会扩展。

这就是二者的最大区别。

同理可以推导到其它的类型,比如short, unsigned short。等等

具体可以通过下面的小例子看看其区别

include <stdio.h>

void f(unsigned char v)

{

char c = v;

unsigned char uc = v;

unsigned int a = c, b = uc;

int i = c, j = uc;

printf("----------------\n");

printf("%%c: %c, %c\n", c, uc);

printf("%%X: %X, %X\n", c, uc);

printf("%%u: %u, %u\n", a, b);

printf("%%d: %d, %d\n", i, j);

}

int main(int argc, char *argv[])

{

f(0x80);

f(0x7F);

return 0;

}

输出结果:

----------------

%c: ?, ?

%X: FFFFFF80, 80

%u: 4294967168, 128

%d: -128, 128

----------------

%c: ,

%X: 7F, 7F

%u: 127, 127

%d: 127, 127

由此可见,最高位若为0时,二者没有区别,若为0时,则有区别了。

 
 
分类: C&C++

用unsigned char 表示字节的更多相关文章

  1. 打印不同对象的字节表示 ( 对int*强制转换成unsigned char*的理解 )

    此文章参考<深入理解计算机系统>P31. 先看如下代码:  12345的十六进制表示为:0x00003039 #include <stdio.h> int main() { ; ...

  2. unsigned char 类型

    在蓝牙4.0的开发中,很多数据类型都用到了 unsigned char ,我觉得用这个类型的一个原因是相比较于整型,它占的空间更少. 比如: unsigned char a = 1;  // 占1个字 ...

  3. char、unsigned char、BYTE

    首先uchar就是BYTE:Typedef unsigned char BYTE: char:就是signed char,是一个字节,8个位.第8位是符号位,所以可以表示-128~127共256个符号 ...

  4. unsigned char 无符号整形 减法运算

    对于一个字节来说: unsigned char :     0  ~  255              0000 0000  ~ 1111 1111 char :-128  ~  127       ...

  5. char 与 unsigned char的本质区别

    在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...

  6. #define XBYTE ((unsigned char volatile xdata *) 0)

    今天在看别人的CAN总线程序的时候,突然发现了这么一句宏定义:#define XBYTE ((unsigned char volatile xdata *) 0),以前都没注意到过.后来查了一下,发现 ...

  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. C++11,控制台输出的一段小程序。

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...

  2. Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu

    While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...

  3. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  4. Blocks实现代理传值

    一.RootViewController: #import "RootViewController.h" #import "SecondViewController.h& ...

  5. Poisson distribution 泊松分布 指数分布

    Poisson distribution - Wikipedia https://en.wikipedia.org/wiki/Poisson_distribution Jupyter Notebook ...

  6. Masonry 动画更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  7. OpenMediaVault 搭建git,ssh无法连接问题

    /************************************************************************* * OpenMediaVault 搭建git,ss ...

  8. bzoj1426 (洛谷P4550) 收集邮票——期望

    题目:https://www.luogu.org/problemnew/show/P4550 推式子……:https://blog.csdn.net/pygbingshen/article/detai ...

  9. 【181】IDL 代码从 Windows 转移到 Linux

    文件夹分隔符,Windows 是“/”,Linux 是“\”,按照程序,需要修改 通过 bash 运行 *.pro 文件,貌似只能运行没有参数的,有参数的需要写入到文件中 idl 的文件不能用大写字母 ...

  10. 【转载】Java方向如何准备BAT技术面试答案(汇总版)

    作者:微信公众号JavaQ链接:https://www.nowcoder.com/discuss/31667?type=0&order=0&pos=11&page=1来源:牛客 ...