char 默认是有符号的还是无符号的?
在《C 完全参考》一书中提到 char
默认情况下是无符号的。
但我正在尝试使用 GCC 和 Visual Studio 来验证这一点。默认情况下,它会将其视为签名。
哪一个是正确的?
In the book "Complete Reference of C" it is mentioned that char
is by default unsigned.
But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default.
Which one is correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
书上写错了。该标准没有指定纯
char
是有符号的还是无符号的。事实上,该标准定义了三种不同的类型:
char
、signed char
和unsigned char
。如果您#include
,然后查看CHAR_MIN
,您可以查明普通char
是否是有符号< /code> 或
unsigned
(如果CHAR_MIN
小于 0 或等于 0),但即便如此,三种类型还是不同的就标准而言。请注意,
char
在这方面很特殊。如果将变量声明为int
,则 100% 相当于将其声明为signed int
。对于所有编译器和体系结构来说都是如此。The book is wrong. The standard does not specify if plain
char
is signed or unsigned.In fact, the standard defines three distinct types:
char
,signed char
, andunsigned char
. If you#include <limits.h>
and then look atCHAR_MIN
, you can find out if plainchar
issigned
orunsigned
(ifCHAR_MIN
is less than 0 or equal to 0), but even then, the three types are distinct as far as the standard is concerned.Do note that
char
is special in this way. If you declare a variable asint
it is 100% equivalent to declaring it assigned int
. This is always true for all compilers and architectures.正如 Alok 指出的,该标准将其留给了实现。
对于 gcc,默认已签名,但您可以使用
-funsigned-char
。 注意:对于 Android NDK 中的 gcc,默认为无符号。您还可以使用-fsigned-char
。在 MSVC 上,默认已签名,但您可以使用
/J
。As Alok points out, the standard leaves that up to the implementation.
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
.On MSVC, the default is signed but you can modify that with
/J
.C99 N1256 草案 6.2.5/15 " Types”对于
char
类型的有符号性有这样的说法:并在脚注中:
C99 N1256 draft 6.2.5/15 "Types" has this to say about the signed-ness of type
char
:and in a footnote:
根据 Dennis Ritchie 所著的《C 编程语言》一书(ANSI C 的事实上的标准书),有符号或无符号的普通字符与机器相关,但可打印字符始终为正数。
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.
根据 C 标准,普通 char 的符号是“实现定义的”。
一般来说,实现者会选择在其架构上实现效率更高的一个。在 x86 系统上,char 通常是有符号的。在 ARM 系统上,它通常是未签名的(Apple iOS 是一个例外)。
According to the C standard the signedness of plain char is "implementation defined".
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).
现在,我们知道该标准将其留给实施。
但是如何检查类型是有符号的还是无符号的,例如 char?
我编写了一个宏来执行此操作:
#define IS_UNSIGNED(t) ((t)~1 > 0)
并使用
gcc
、clang
进行测试代码>和<代码>cl。但我不确定它对于其他情况总是安全的。Now, we known the standard leaves that up to the implementation.
But how to check a type is
signed
orunsigned
, such aschar
?I wrote a macro to do this:
#define IS_UNSIGNED(t) ((t)~1 > 0)
and test it with
gcc
,clang
, andcl
. But I do not sure it's always safe for other cases.