免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 18096 | 回复: 18
打印 上一主题 下一主题

char 和 unsigned char有什么区别? [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-06-18 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-02 10:26 |只看该作者 |倒序浏览
请问unsigned char是什么类型?主要用于哪些地方?
和char 有什么区别?


什么时候用char, 什么时候用unsigned char?
char 是指哪些字符,unsigned char 是指哪些类型?

论坛徽章:
0
2 [报告]
发表于 2007-02-02 10:30 |只看该作者
搜搜
     http://www.cppreference.com/data_types.html

http://www.arm.linux.org.uk/docs/faqs/signedchar.php


  1. > consider this simple program:
  2.         > int main(void)
  3.         > {
  4.         >     char i = -1;
  5.         >     printf("%d\n", i);
  6.         >     return 0;
  7.         > }
  8.         >
  9.         > The print out is 255 in stead of -1, unless I define i as
  10.         > signed char i;
  11.         > then I get the "-1" print out.

复制代码


  1. The character type is used to store characters - typically ASCII characters but not always. For example:

  2. char menuSelection = 'q';
  3. char userInput = '3';


  4. Note how a character is enclosed within single quotes. We can also assign numeric values to variables of character type:

  5. char chNumber = 26;


  6. We can declare signed and unsigned characters, where signed characters can have positive and negative values, and unsigned characters can only contain positive values.

  7. signed char myChar = 100;
  8. signed char newChar = -43;
  9. unsigned char yourChar = 200;


  10. Note that if we use a plain char, neither signed nor unsigned:

  11. char dataValue = 27;


  12. it may differ between compilers as to whether it behaves as a signed or unsigned character type. On some compilers it may accept positive and negative values, on others it may only accept positive values. Refer to your compiler documentation to see which applies.

  13. A char is guaranteed to be at least 8 bits in size. C++ also provides the data type wchar_t, a wide character type typically used for large character sets.

  14. An array of characters can be used to contain a C-style string in C++. For example:

  15. char aString[] = "This is a C-style string";


  16. Note that C++ also provides a string class that has advantages over the use of character arrays
复制代码

[ 本帖最后由 bsdc 于 2007-2-2 10:44 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2007-02-02 10:51 |只看该作者
找到句话

  1. In the ANSI C Draft Standard, the keyword signed was added, introducing a signed char type for all platforms. The new keyword solved the problem of not being able to use signed char portably, but at this point the standard committee could not mandate plain char to be signed. It would break a lot of code and upset vendors as well as users.

  2. The comprimise was to make signed char a type distinct from the two existing character types, while requiring char to have the same representation and values as either signed char or unsigned char. In other words, a char must look exactly like a signed char or unsigned char to the hardware; which one is implementation-defined. C++ later adopted this compromise for compatibility with C, so both languages now have three distinct char types.

复制代码

说明char, unsigned char, signed char是三种类型,哈哈,char不像int 一样默认为signed的,主要是为了不使得前面很多程序不至于被颠覆

论坛徽章:
0
4 [报告]
发表于 2007-02-02 19:09 |只看该作者

hehe

谢谢,学习了

论坛徽章:
0
5 [报告]
发表于 2007-02-02 22:21 |只看该作者
char 不是signed char那是什么呢,编译器相关?

论坛徽章:
0
6 [报告]
发表于 2007-02-02 22:22 |只看该作者
原帖由 blackuhlan 于 2007-2-2 22:21 发表
char 不是signed char那是什么呢,编译器相关?

对,是实现相关的,就是上面的plain char

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
7 [报告]
发表于 2007-02-03 09:52 |只看该作者
char跟编译器的设置有关,可以是signed char(一般默认),也可以是unsigned char

论坛徽章:
0
8 [报告]
发表于 2007-02-03 10:38 |只看该作者
unsigned char  通常用于需要做2进制操作时很有用
char 当然是字符传操作了

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
9 [报告]
发表于 2007-02-03 22:07 |只看该作者
to LZ:什么时候用unsigned int,什么时候用signed int?

论坛徽章:
0
10 [报告]
发表于 2007-02-03 23:57 |只看该作者
一般情况下用来没什么区别,可能只是在位运算里面有点用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP