C语言 默认情况下,char 是有符号的还是无符号的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054939/
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
Is char signed or unsigned by default?
提问by C Learner
In the book "Complete Reference of C" it is mentioned that charis by default unsigned.
在“C 的完整参考”一书中提到,char默认情况下是无符号的。
But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signedby default.
但我试图用 GCC 和 Visual Studio 验证这一点。默认情况下将其视为已签名。
Which one is correct?
哪一个是正确的?
回答by Alok Singhal
The book is wrong. The standard does not specify if plain charis signed or unsigned.
书错了。该标准没有指定plainchar是有符号的还是无符号的。
In fact, the standard defines three distinct types: char, signed char, and unsigned char. If you #include <limits.h>and then look at CHAR_MIN, you can find out if plain charis signedor unsigned(if CHAR_MINis less than 0 or equal to 0), but even then, the three types are distinctas far as the standard is concerned.
实际上,标准定义了三种不同的类型:char,signed char,和unsigned char。如果您#include <limits.h>然后查看CHAR_MIN,您可以找出是否plainchar是signed或unsigned(如果CHAR_MIN小于0 或等于0),但即便如此,就标准而言,这三种类型是不同的。
Do note that charis special in this way. If you declare a variable as intit is 100% equivalent to declaring it as signed int. This is always true for all compilers and architectures.
请注意,char这种方式很特别。如果将变量声明int为 100% 等同于将其声明为signed int. 对于所有编译器和体系结构,这始终是正确的。
回答by R Samuel Klatchko
As Alok points out, the standard leaves that up to the implementation.
正如Alok 指出的那样,标准将其留给了实施。
For gcc, the default is signed, but you can modify that with -funsigned-char. note:for gcc in Android NDK, the default is unsigned. You can also explicitly ask for signed characters with -fsigned-char.
对于 gcc,默认是有符号的,但您可以使用-funsigned-char. 注意:对于 Android NDK 中的 gcc,默认为 unsigned。您还可以使用 明确要求签名字符-fsigned-char。
On MSVC, the default is signed but you can modify that with /J.
在 MSVC 上,默认已签名,但您可以使用/J.
回答by Michael Burr
C99 N1256 draft6.2.5/15 "Types" has this to say about the signed-ness of type char:
C99 N1256 草案6.2.5/15 "Types" 对 type 的签名有这样的说法char:
The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.
实现应定义 char 以具有与有符号字符或无符号字符相同的范围、表示和行为。
and in a footnote:
并在脚注中:
CHAR_MIN, defined in<limits.h>, will have one of the values0orSCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made,charis a separate type from the other two and is not compatible with either.
CHAR_MIN中定义的<limits.h>将具有值0或 之一SCHAR_MIN,这可用于区分这两个选项。无论做出char何种选择,它都是与其他两个不同的类型,并且与任何一个都不兼容。
回答by Ravi Rathi
According to The C Programming Language book by Dennis Ritchie which is the de-facto standard book for ANSI C, plain chars either signed or unsigned are machine dependent, but printable characters are always positive.
根据 Dennis Ritchie 的 The C Programming Language 一书,这是 ANSI C 的事实上的标准书,有符号或无符号的纯字符依赖于机器,但可打印的字符总是正数。
回答by plugwash
According to the C standard the signedness of plain char is "implementation defined".
根据 C 标准,普通字符的符号是“实现定义的”。
In general implementors chose whichever was more efficient to implement on their architecture. On x86 systems char is generally signed. On arm systems it is generally unsigned (Apple iOS is an exception).
一般来说,实现者选择在他们的架构上实现效率更高的那个。在 x86 系统上,char 通常是有符号的。在 arm 系统上,它通常是未签名的(Apple iOS 是一个例外)。
回答by BoQ
According to "The C++ Programming Language" by Bjarne Stroustrup, charis "implementation defined". It can be signed charor unsigned chardepending on implementation. You can check whether charis signed or not by using std::numeric_limits<char>::is_signed.
根据 Bjarne Stroustrup 的“C++ 编程语言”,char是“实现定义”。它可以是signed char或unsigned char取决于实现。您可以char使用std::numeric_limits<char>::is_signed.
回答by u2086105
Now, we known the standard leaves that up to the implementation.
现在,我们知道标准将其留给了实施。
But how to check a type is signedor unsigned, such as char?
但是如何检查一个类型是signedor unsigned,比如char?
I wrote a macro to do this:
我写了一个宏来做到这一点:
#define IS_UNSIGNED(t) ((t)~1 > 0)
#define IS_UNSIGNED(t) ((t)~1 > 0)
and test it with gcc, clang, and cl. But I do not sure it's always safe for other cases.
与测试gcc,clang以及cl。但我不确定在其他情况下它总是安全的。

![C语言 格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'size_t' [-Wformat]](/res/img/loading.gif)