是否存在 sizeof(char) != 1 或至少 CHAR_BIT > 的机器8?
是否存在 sizeof(char) != 1
的机器(或编译器)?
C99 标准 是否规定标准合规性实现中的 sizeof(char)
必须恰好为 1?如果是的话,请给我章节编号和引用。
更新: 如果我有一台机器(CPU),它不能寻址字节(最小读取是 4 个字节,对齐),但只有 4 个字节(uint32_t
),可以为此编译器机器将 sizeof(char)
定义为 4?sizeof(char)
将为 1,但 char 将有 32 位 (< code>CHAR_BIT 宏)
更新2: 但 sizeof 结果不是字节!它是 CHAR 的大小。 char 可以是 2 个字节,或者(可能是)7 位?
更新3: 好的。所有机器都有sizeof(char) == 1
。但是什么机器有CHAR_BIT>? 8 ?
Are there machines (or compilers), where sizeof(char) != 1
?
Does C99 standard says that sizeof(char)
on standard compliance implementation MUST be exactly 1? If it does, please, give me section number and citation.
Update:
If I have a machine (CPU), which can't address bytes (minimal read is 4 bytes, aligned), but only 4-s of bytes (uint32_t
), can compiler for this machine define sizeof(char)
to 4?sizeof(char)
will be 1, but char will have 32 bits (CHAR_BIT
macros)
Update2:
But sizeof result is NOT a BYTES ! it is the size of CHAR. And char can be 2 byte, or (may be) 7 bit?
Update3:
Ok. All machines have sizeof(char) == 1
. But what machines have CHAR_BIT > 8
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C99 中,它始终为 1,第 6.5.3.4 节:
编辑:不是您问题的一部分,而是出于 哈比森和斯蒂尔的兴趣。 C:参考手册,第三版,Prentice Hall,1991(c99 之前)第 14 页。 148:
编辑:为了回答您更新的问题,Harbison 和 Steele 的以下问题和答案是相关的(同上,第 6 章的 Ex. 4):
答案(同上,第 382 页):
虽然这并没有具体解决这样的情况:假设字节是 8 位,而
char
是其中的 4 个字节(对于 c99 实际上是不可能的)定义,见下文),事实上sizeof(char) = 1
总是从 c99 标准以及 Harbison 和 Steele 中清楚地看出。编辑:事实上(这是为了回答你的upd 2问题),就c99而言,
sizeof(char)
是以字节为单位,再次来自第6.5.3.4节:与上面的引用相结合,8位字节和
char
作为其中4个字节是不可能的:对于c99,字节与char
相同代码>.回答你提到的 7 位
char
的可能性:这在 c99 中是不可能的。根据标准第 5.2.4.2.1 节,最小值为 8:It is always one in C99, section 6.5.3.4:
Edit: not part of your question, but for interest from Harbison and Steele's. C: A Reference Manual, Third Edition, Prentice Hall, 1991 (pre c99) p. 148:
Edit: In answer to your updated question, the following question and answer from Harbison and Steele is relevant (ibid, Ex. 4 of Ch. 6):
Answer (ibid, p. 382):
While this does not specifically address a case where, say bytes are 8 bits and
char
are 4 of those bytes (actually impossible with the c99 definition, see below), the fact thatsizeof(char) = 1
always is clear from the c99 standard and Harbison and Steele.Edit: In fact (this is in response to your upd 2 question), as far as c99 is concerned
sizeof(char)
is in bytes, from section 6.5.3.4 again:so combined with the quotation above, bytes of 8 bits and
char
as 4 of those bytes is impossible: for c99 a byte is the same as achar
.In answer to your mention of the possibility of a 7 bit
char
: this is not possible in c99. According to section 5.2.4.2.1 of the standard the minimum is 8:不存在
sizeof(char)
为 4 的机器。它始终是 1 个字节。该字节可能包含 32 位,但对于 C 编译器而言,它是一个字节。有关更多详细信息,我实际上将向您指出 C++ 常见问题解答 26.6。该链接很好地涵盖了它,我相当确定 C++ 从 C 中获得了所有这些规则。您还可以查看 comp.lang.c FAQ 8.10 适用于大于 8 位的字符。是的,是字节。我再说一遍。根据 C 编译器的规定,
sizeof(char)
为 1 个字节。人们俗称的字节(8 位)不一定与 C 编译器所说的字节相同。 C 字节中的位数根据您的机器架构而有所不同。它还保证至少为 8。There are no machines where
sizeof(char)
is 4. It's always 1 byte. That byte might contain 32 bits, but as far as the C compiler is concerned, it's one byte. For more details, I'm actually going to point you at the C++ FAQ 26.6. That link covers it pretty well and I'm fairly certain C++ got all of those rules from C. You can also look at comp.lang.c FAQ 8.10 for characters larger than 8 bits.Yes, it is bytes. Let me say it again.
sizeof(char)
is 1 byte according to the C compiler. What people colloquially call a byte (8 bits) is not necessarily the same as what the C compiler calls a byte. The number of bits in a C byte varies depending on your machine architecture. It's also guaranteed to be at least 8.PDP-10
和 PDP-11是。更新:
PDP-10 没有 C99 编译器。Analog Devices 32 位 SHARC DSP 的某些型号的 CHAR_BIT=32,并且
TMS32F28xx 的 Texas Instruments DSP 具有 CHAR_BIT=16,据报道 。
更新:有 GCC 3.2 for PDP-10字符位=9
(检查该存档中的 include/limits.h)。
PDP-10
and PDP-11was.Update:
there like no C99 compilers for PDP-10.Some models of Analog Devices 32-bit SHARC DSP have CHAR_BIT=32, and
Texas Instruments DSP from TMS32F28xx have CHAR_BIT=16, reportedly.
Update: There is GCC 3.2 for PDP-10 with CHAR_BIT=9
(check include/limits.h in that archive).