char 默认是有符号的还是无符号的?

发布于 2024-08-17 07:36:09 字数 137 浏览 6 评论 0原文

在《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

℉服软 2024-08-24 07:36:09

书上写错了。该标准没有指定纯 char 是有符号的还是无符号的。

事实上,该标准定义了三种不同的类型:charsigned charunsigned 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, and unsigned char. If you #include <limits.h> and then look at CHAR_MIN, you can find out if plain char is signed or unsigned (if CHAR_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 as int it is 100% equivalent to declaring it as signed int. This is always true for all compilers and architectures.

橙味迷妹 2024-08-24 07:36:09

正如 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.

傾城如夢未必闌珊 2024-08-24 07:36:09

C99 N1256 草案 6.2.5/15 " Types”对于 char 类型的有符号性有这样的说法:

实现应将 char 定义为与有符号字符或无符号字符具有相同的范围、表示形式和行为。

并在脚注中:

CHAR_MIN,在 中定义,将具有值 0SCHAR_MIN 之一>,这可以用来区分这两个选项。无论做出何种选择,char 都是与其他两种不同的类型,并且与任何一种都不兼容。

C99 N1256 draft 6.2.5/15 "Types" has this to say about the signed-ness of type char:

The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

and in a footnote:

CHAR_MIN, defined in <limits.h>, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.

凉栀 2024-08-24 07:36:09

根据 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.

锦上情书 2024-08-24 07:36:09

根据 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).

小清晰的声音 2024-08-24 07:36:09

现在,我们知道该标准将其留给实施。

但是如何检查类型是有符号的还是无符号的,例如 char?

我编写了一个宏来执行此操作:

#define IS_UNSIGNED(t) ((t)~1 > 0)

并使用 gccclang 进行测试代码>和<代码>cl。但我不确定它对于其他情况总是安全的。

Now, we known the standard leaves that up to the implementation.

But how to check a type is signed or unsigned, such as char?

I wrote a macro to do this:

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文