为什么字符的登录内斯用C没有定义? [英] why is char's sign-ness not defined in C?

查看:100
本文介绍了为什么字符的登录内斯用C没有定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准规定:

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

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

这三种类型CHAR,符号的字符,以及
  unsigned char型统​​称
  字符类型。该
  实现应定义为CHAR
  具有相同的范围,再presentation,
  和行为要么签署char或
  unsigned char型。

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.

事实上 GCC ,根据目标平台定义。

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.

不是那么多,ANSI C(C99之前),只有一个字节大小的类型为char,因此使用CHAR数学有时是不可避免的。所以说:一个人永远不能使用CHAR数学并非如此真实。如果是这样的情况下,更理智的决定是包括三种类型的字符 UBYTE 为sbyte

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?

推荐答案

素具有不确定的签署岬字符使编译器选择任何重新presentation是目标架构更高效:在某些体系结构,零扩展一个字节的值设置为INT的大小,需要更少的操作(从而使普通的char'签名'),而对他人的指令集使符号扩展更自然,并作为签署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.

这篇关于为什么字符的登录内斯用C没有定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆