首页 > 学技术 > 技术网文 > C/C++ > 正文

[保留] char 和 unsigned char有什么区别?


来源 chinaunix.net kuqin整理

请问unsigned char是什么类型?主要用于哪些地方?
和char 有什么区别?


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



 bsdc 回复于:2007-02-02 10:30:56

搜搜
:D     http://www.cppreference.com/data_types.html

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


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




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

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


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

char chNumber = 26; 


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

signed char myChar = 100; 
signed char newChar = -43; 
unsigned char yourChar = 200; 


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

char dataValue = 27; 


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. 

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. 

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

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


Note that C++ also provides a string class that has advantages over the use of character arrays


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


 bsdc 回复于:2007-02-02 10:51:05

找到句话

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.

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的,主要是为了不使得前面很多程序不至于被颠覆


 3040602024 回复于:2007-02-02 19:09:45

谢谢,学习了


 blackuhlan 回复于:2007-02-02 22:21:27

char 不是signed char那是什么呢,编译器相关?


 tyc611 回复于:2007-02-02 22:22:27

引用:原帖由 blackuhlan 于 2007-2-2 22:21 发表
char 不是signed char那是什么呢,编译器相关? 


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


 cobras 回复于:2007-02-03 09:52:23

char跟编译器的设置有关,可以是signed char(一般默认),也可以是unsigned char


 geba168 回复于:2007-02-03 10:38:22

unsigned char  通常用于需要做2进制操作时很有用
char 当然是字符传操作了


 cjaizss 回复于:2007-02-03 22:07:24

to LZ:什么时候用unsigned int,什么时候用signed int?


 marxn 回复于:2007-02-03 23:57:33

一般情况下用来没什么区别,可能只是在位运算里面有点用。


 cobras 回复于:2007-02-04 11:22:51

需要负数运算时使用signed char,不需要时使用unsigned char


 koolcoy 回复于:2007-02-23 03:16:18

引用:原帖由 cobras 于 2007-2-4 11:22 发表
需要负数运算时使用signed char,不需要时使用unsigned char 


不同意, 因该是只要不牵涉到整数的二进制表示的问题的时候就使用signed int(如果int的范围不够就使用long, 再不行就使用高精度运算库). unsigned不是易驯服的东西.




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=894918
转载请注明作者名及原文出处



收藏本页到: