为什么 C 中没有定义 char 的符号?

发布于 2024-07-22 06:24:13 字数 676 浏览 6 评论 0原文

C 标准规定:

ISO/IEC 9899:1999, 6.2.5.15(第 49 页)

三种类型 char、signed char 和 unsigned char 统称为 字符类型。 这 实现应将 char 定义为 具有相同的范围、表示形式, 和行为为signed char 或 无符号字符。

事实上 gcc 根据目标平台定义了它。

我的问题是,为什么标准要这样做? 除了可怕且难以发现的错误之外,我看不出任何不明确的类型定义会产生任何结果。

更重要的是,在 ANSI C(C99 之前)中,唯一的字节大小类型是 char,因此使用 char 进行数学计算有时是不可避免的。 因此,“永远不要使用 char 进行数学计算”的说法并不正确。 如果是这种情况,更明智的决定是包含三种类型“charubytesbyte”。

是否有原因,或者只是一些奇怪的向后兼容性陷阱,以便允许将糟糕的(但常见的)编译器定义为标准兼容?

The C standard states:

ISO/IEC 9899:1999, 6.2.5.15 (p. 49)

The three types char, signed char, and
unsigned char are collectively called
the character types. The
implementation shall define char to
have the same range, representation,
and behavior as either signed char or
unsigned char.

And indeed gcc define that according to target platform.

My question is, why does the standard do that? I can see nothing that can come out of ambiguous type definition, except of hideous and hard to spot bugs.

More than so, in ANSI C (before C99), the only byte-sized type is char, so using char for math is sometimes inevitable. So saying "one should never use char for math" is not so true. If that was the case, a saner decision was to include three types "char,ubyte,sbyte".

Is there a reason for that, or is it just some weird backwards-compatibility gotcha, in order to allow bad (but common) compilers to be defined as standard compatible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

花开雨落又逢春i 2024-07-29 06:24:13

具有未指定符号的“普通”char 允许编译器选择对目标体系结构更有效的表示形式:在某些体系结构上,将一字节值零扩展到“int”的大小需要较少的操作(从而使普通 char ' unsigned'),而在其他指令集上,符号扩展更加自然,并且普通 char 被实现为有符号。

"Plain" char having unspecified signed-ness allows compilers to select whichever representation is more efficient for the target architecture: on some architectures, zero extending a one-byte value to the size of "int" requires less operations (thus making plain char 'unsigned'), while on others the instruction set makes sign-extending more natural, and plain char gets implemented as signed.

生活了然无味 2024-07-29 06:24:13

也许历史上有些实现的“char”是有符号的,有些是无符号的,因此为了与两者兼容,他们不能将其定义为其中之一。

Perhaps historically some implementations' "char" were signed and some were unsigned, and so to be compatible with both they couldn't define it as one or the other.

你的他你的她 2024-07-29 06:24:13

在那些美好的过去,C 被定义,字符世界是 7 位,因此符号位可以用于其他事物(例如 EOF)

in those good old days C was defined, the character world was 7bit, so the sign-bit could be used for other things (like EOF)

执手闯天涯 2024-07-29 06:24:13

我想(从我的脑海中)他们的想法是这样的:

如果您关心 char 的符号(将其用作字节),您应该明确选择有符号或无符号字符。

I suppose (out of the top of my head) that their thinking was along the following lines:

If you care about the sign of char (using it as a byte) you should explicitly choose signed or unsigned char.

泪之魂 2024-07-29 06:24:13

在某些机器上,有符号的 char 太小,无法容纳 C 字符集中的所有字符(字母、数字、标准标点符号等)。在此类机器上,“char”必须是无符号的。 在其他机器上,unsigned char 可以保存大于signed int 的值(因为 char 和 int 大小相同)。 在这些机器上,必须对“char”进行签名。

On some machines, a signed char would be too small to hold all the characters in the C character set (letters, digits, standard punctuation, etc.) On such machines, 'char' must be unsigned. On other machines, an unsigned char can hold values larger than a signed int (since char and int are the same size). On those machines, 'char' must be signed.

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