Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316381
  • 博文数量: 81
  • 博客积分: 1810
  • 博客等级: 上尉
  • 技术积分: 725
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 17:38
文章分类

全部博文(81)

文章存档

2016年(4)

2015年(11)

2014年(16)

2013年(37)

2012年(11)

2011年(2)

我的朋友

分类: C/C++

2014-03-05 21:34:48





ANSI C 提供了3种字符类型,分别是char、signed char、unsigned char
char相当于signed char或者unsigned char,但是这取决于编译器!
这三种字符类型都是按照1个字节存储的,可以保存256个不同的值。
signed char取值范围是 -128 到 127
unsigned char 取值范围是 0 到 255

但是char究竟相当于signed char呢还是相当于unsigned char呢??
这就是char和int的不同之处!
int==signed int,但是char不能简单以为==signed char

要确定char究竟等同什么要基于不同的编译器做测试
大多数机器使用补码来存储整数,在这些机器中按照整数类型存储的-1的所有位均是1
假设我的机器也是如此存储,就能据此判断char究竟是等于signed char还是unsigned char

-funsigned-char
-fno-signed-char
-fsigned-char
-fno-unsigned-char
  这四个参数是对char类型进行设置,决定将char类型设置成unsigned char(前两个参
数)或者 signed char(后两个参数)
gcc选项解释
       -funsigned-char
           Let the type "char" be unsigned, like "unsigned char".

           Each kind of machine has a default for what "char" should be.  It
           is either like "unsigned char" by default or like "signed char" by
           default.

           Ideally, a portable program should always use "signed char" or
           "unsigned char" when it depends on the signedness of an object.
           But many programs have been written to use plain "char" and expect
           it to be signed, or expect it to be unsigned, depending on the
           machines they were written for.  This option, and its inverse, let
           you make such a program work with the opposite default.

           The type "char" is always a distinct type from each of "signed
           char" or "unsigned char", even though its behavior is always just
           like one of those two.



阅读(1597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~