C语言 有符号/无符号字符之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4337217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:16:26  来源:igfitidea点击:

Difference between signed / unsigned char

ctypesunsigned

提问by Chiggins

So I know that the difference between a signed intand unsigned intis that a bit is used to signify if the number if positive or negative, but how does this apply to a char? How can a character be positive or negative?

所以我知道 asigned intunsigned inta之间的区别是一个位用于表示数字是正数还是负数,但这如何适用于 a char?一个角色如何是积极的或消极的?

采纳答案by AnT

There's no dedicated "character type" in C language. charis an integer type, same (in that regard) as int, shortand other integer types. charjust happens to be the smallest integer type. So, just like any other integer type, it can be signed or unsigned.

C 语言中没有专门的“字符类型”。char是整数类型,相同的(在这方面)为intshort与其它整数类型。char恰好是最小的整数类型。因此,就像任何其他整数类型一样,它可以是有符号的或无符号的。

It is true that (as the name suggests) charis mostly intended to be used to represent characters. But characters in C are represented by their integer "codes", so there's nothing unusual in the fact that an integer type charis used to serve that purpose.

确实(顾名思义)char主要用于表示字符。但是 C 中的字符由它们的整数“代码”表示,因此使用整数类型char来实现这一目的并没有什么不寻常的地方。

The only general difference between charand other integer types is that plain charis not synonymous with signed char, while with other integer types the signedmodifier is optional/implied.

char与其他整数类型之间唯一的一般区别是,plainchar不是 同义词signed char,而对于其他整数类型,signed修饰符是可选的/隐含的。

回答by Simple Fellow

I slightly disagree with the above. The unsigned charsimply means: Use the most significant bit instead of treating it as a bit flag for +/- sign when performing arithmetic operations.

我有点不同意上面的说法。在unsigned char简单的方法:用最显著位,而不是执行算术运算时,把它当作一个位标志为+/-号。

It makes significance if you use charas a number for instance:

例如,如果您将其char用作数字,则意义重大:

typedef char BYTE1;
typedef unsigned char BYTE2;

BYTE1 a;
BYTE2 b;

For variable a, only 7 bits are available and its range is (-127 to 127) = (+/-)2^7 -1. For variable ball 8 bits are available and the range is 0 to 255 (2^8 -1).

对于 variable a,只有 7 位可用,其范围是 (-127 到 127) = (+/-)2^7 -1。对于变量,b所有 8 位都可用,范围是 0 到 255 (2^8 -1)。

If you use charas character, "unsigned" is completely ignored by the compiler just as comments are removed from your program.

如果使用charas 字符,编译器会完全忽略“无符号”,就像从程序中删除注释一样。

回答by DrAl

There are three char types: (plain) char, signed charand unsigned char. Any char is usually an 8-bit integer* and in that sense, a signedand unsigned charhave a useful meaning (generally equivalent to uint8_tand int8_t). When used as a character in the sense of text, use a char(also referred to as a plain char). This is typically a signed charbut can be implemented either way by the compiler.

有三种字符类型:(普通)charsigned charunsigned char。任何字符通常是一个 8 位整数*,从这个意义上说, a signedandunsigned char具有有用的含义(通常等同于uint8_tand int8_t)。当用作文本意义上的字符时,请使用 a char(也称为普通字符)。这通常是一个,signed char但可以由编译器以任何一种方式实现。

* Technically, a char can be any size as long as sizeof(char)is 1, but it is usually an 8-bit integer.

* 从技术上讲,只要sizeof(char)是 1,字符就可以是任意大小,但通常是 8 位整数。

回答by user2376256

Representation is the same, the meaning is different. e.g, 0xFF, it both represented as "FF". When it is treated as "char", it is negative number -1; but it is 255 as unsigned. When it comes to bit shifting, it is a big difference since the sign bit is not shifted. e.g, if you shift 255 right 1 bit, it will get 127; shifting "-1" right will be no effect.

表象是一样的,意思是不同的。例如,0xFF,它都表示为“FF”。当它被视为“char”时,它是负数-1;但它是 255 作为未签名。当谈到位移时,这是一个很大的不同,因为符号位没有位移。例如,如果您将 255 右移 1 位,它将得到 127;右移“-1”将没有效果。

回答by supercat

A signed charis a signed value which is typically smaller than, and is guaranteed not to be bigger than, a short. An unsigned charis an unsigned value which is typically smaller than, and is guaranteed not to be bigger than, a short. A type charwithout a signedor unsignedqualifier may behave as either a signed or unsigned char; this is usually implementation-defined, but there are a couple of cases where it is not:

Asigned char是一个有符号值,它通常小于 a ,并且保证不大于 a short。Anunsigned char是一个无符号值,它通常小于 a ,并且保证不大于 a shortchar没有 asignedunsigned限定符的类型可以表现为有符号或无符号char;这通常是实现定义的,但有几种情况不是:

  1. If, in the target platform's character set, any of the characters required by standard C would map to a code higher than the maximum `signed char`, then `char` must be unsigned.
  2. If `char` and `short` are the same size, then `char` must be signed.
  1. 如果在目标平台的字符集中,标准 C 所需的任何字符将映射到高于最大 `signed char` 的代码,那么 `char` 必须是无符号的。
  2. 如果 `char` 和 `short` 大小相同,则必须对 `char` 进行签名。

Part of the reason there are two dialects of "C" (those where charis signed, and those where it is unsigned) is that there are some implementations where charmustbe unsigned, and others where it mustbe signed.

部分原因也有“C”的两种方言(那些char被签署,而这些地方是无符号)是有一些实现中char必须是无符号,其他地方必须签字。

回答by Hyman

This because a charis stored at all effects as a 8-bit number. Speaking about a negative or positive chardoesn't make sense if you consider it an ASCII code (which can be just signed*) but makes sense if you use that charto store a number, which could be in range 0-255 or in -128..127 according to the 2-complement representation.

这是因为 achar在所有效果中都存储为 8 位数字。char如果您认为它是 ASCII 代码(可以只是带符号的*),则谈论负数或正数没有意义,但如果您使用它char来存储数字(可能在 0-255 或 -128 范围内)则有意义..127 根据 2 补码表示。

*: it can be also unsigned, it actually depends on the implementation I think, in that case you will have access to extended ASCII charset provided by the encoding used

*:它也可以是无符号的,它实际上取决于我认为的实现,在这种情况下,您将可以访问所用编码提供的扩展 ASCII 字符集

回答by ?imon Tóth

The same way how an intcan be positive or negative. There is no difference. Actually on many platforms unqualified charis signed.

以同样的方式 aint可以是积极的或消极的。没有区别。其实在很多平台上都是不合格char的签约。

回答by Stuart Golodetz

The same way -- e.g. if you have an 8-bit char, 7 bits can be used for magnitude and 1 for sign. So an unsigned char might range from 0 to 255, whilst a signed char might range from -128 to 127 (for example).

同样的方式——例如,如果您有一个 8 位字符,则 7 位可用于大小,1 位可用于符号。因此,无符号字符的范围可能从 0 到 255,而有符号字符的范围可能从 -128 到 127(例如)。