C语言 是否有机器,其中 sizeof(char) != 1,或至少 CHAR_BIT > 8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2215445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:23:59  来源:igfitidea点击:

Are there machines, where sizeof(char) != 1, or at least CHAR_BIT > 8?

ccharstandardssizeofc99

提问by osgx

Are there machines (or compilers), where sizeof(char) != 1?

是否有机器(或编译器),在哪里sizeof(char) != 1

Does C99 standardsays that sizeof(char)on standard compliance implementation MUST be exactly 1? If it does, please, give me section number and citation.

C99 标准是否说sizeof(char)标准合规性实现必须恰好为 1?如果有,请给我章节号和引文。

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_BITmacros)

更新:如果我有一台机器(CPU),它不能寻址字节(最小读取是 4 个字节,对齐),但只有 4 个字节(uint32_t),这台机器的编译器可以定义sizeof(char)为 4 吗?sizeof(char)将是 1,但 char 将有 32 位CHAR_BIT宏)

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?

更新 2:但 sizeof 结果不是 BYTES !它是CHAR的大小。而 char 可以是 2 字节,或者(可能是)7 位?

Update3:Ok. All machines have sizeof(char) == 1. But what machines have CHAR_BIT > 8?

更新3:好的。所有机器都有sizeof(char) == 1。但是什么机器有CHAR_BIT > 8

采纳答案by Ramashalanka

It is always one in C99, section 6.5.3.4:

在 C99 第 6.5.3.4 节中始终为 1:

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

当应用于具有 char、unsigned char 或 signed char 类型(或其限定版本)的操作数时,结果为 1。

Edit: not part of your question, but for interest from Harbison and Steele, 3rd ed. (pre c99) p. 148:

编辑:不是你的问题的一部分,而是 Harbison 和 Steele,第 3 版的兴趣。(c99 之前) p. 148:

A storage unit is taken to be the amount of storage occupied by one character; the size of an object of type charis therefore 1.

一个存储单元是指一个字符占用的存储空间;char因此类型对象的大小为1。

Edit: In answer to your updated question, the following question and answer from Harbison and Steele is relevant (ibid, Ex. 4 of Ch. 6):

编辑:为了回答您更新的问题,Harbison 和 Steele 的以下问题和答案是相关的(同上,第 6 章的第 4 部分):

Is it allowable to have a C implementation in which type charcan represent values ranging from -2,147,483,648 through 2,147,483,647? If so, what would be sizeof(char)under that implementation? What would be the smallest and largest ranges of type int?

是否允许有一个 C 实现,其中类型char可以表示从 -2,147,483,648 到 2,147,483,647 的值?如果是这样,那么sizeof(char)在该实施下会是什么?type 的最小和最大范围是int多少?

Answer (ibid, p. 382):

答案(同上,第 382 页):

It is permitted (if wasteful) for an implementation to use 32 bits to represent type char. Regardless of the implementation, the value of sizeof(char)is always 1.

允许(如果浪费)实现使用 32 位来表示 type char。无论实现如何,的值 sizeof(char)始终为 1。

While this does not specifically address a case where, say bytes are 8 bits and charare 4 of those bytes (actually impossible with the c99 definition, see below), the fact that sizeof(char) = 1always is clear from the c99 standard and Harbison and Steele.

虽然这并没有专门解决一种情况,比如字节是 8 位并且char是其中的 4 个字节(c99 定义实际上不可能,请参见下文),sizeof(char) = 1但从 c99 标准和 Harbison 和 Steele 中可以清楚地看出这一点。

Edit: In fact (this is in response to your upd 2 question), as far as c99 is concerned sizeof(char)isin bytes, from section 6.5.3.4 again:

编辑:事实上(这是对你的 upd 2 问题的回应),就 c99 而言sizeof(char)以字节单位,再次来自第 6.5.3.4 节:

The sizeof operator yields the size (in bytes) of its operand

sizeof 运算符产生其操作数的大小(以字节为单位)

so combined with the quotation above, bytes of 8 bits and charas 4 of those bytes is impossible: for c99 a byte is the same as a char.

因此结合上面的引用,8 位字节和其中char4 个字节是不可能的:对于 c99,一个字节与char.

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 minimumis 8:

回答您提到 7 位的可能性char:这在 c99 中是不可能的。根据标准的第 5.2.4.2.1 节,最小值为 8:

Their implementation-defined values shall be equal or greater[my emphasis] in magnitude to those shown, with the same sign.

它们的实现定义的值应等于或大于[我强调] 所示的值,并具有相同的符号。

— number of bits for smallest object that is not a bit-field (byte)

— 不是位域的最小对象的位数(字节)

 **CHAR_BIT 8**

— minimum value for an object of type signed char

— 符号类型对象的最小值

**SCHAR_MIN -127//?(27?1)** 

— maximum value for an object of type signed char

— 符号类型对象的最大值

**SCHAR_MAX +127//27?1** 

— maximum value for an object of type unsigned char

— unsigned char 类型对象的最大值

**UCHAR_MAX 255//28?1** 

— minimum value for an object of type char

— char 类型对象的最小值

**CHAR_MIN**    see below 

— maximum value for an object of type char

— char 类型对象的最大值

**CHAR_MAX**    see below

[...]

[...]

If the value of an object of type char is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX. The value UCHAR_MAX shall equal 2^CHAR_BIT ? 1.

如果在表达式中使用 char 类型对象的值作为有符号整数处理,则 CHAR_MIN 的值应与 SCHAR_MIN 的值相同,CHAR_MAX 的值应与 SCHAR_MAX 的值相同。否则,CHAR_MIN 的值为0,CHAR_MAX 的值应与UCHAR_MAX 的值相同。值 UCHAR_MAX 应等于 2^CHAR_BIT 吗?1.

回答by Michael Kristofik

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.10for characters larger than 8 bits.

没有机器sizeof(char)是 4。它总是 1 个字节。该字节可能包含 32 位,但就 C 编译器而言,它是一个字节。有关更多详细信息,我实际上将向您指出C++ FAQ 26.6。该链接很好地涵盖了它,我相当肯定 C++ 从 C 中获得了所有这些规则。对于大于 8 位的字符,您还可以查看comp.lang.c FAQ 8.10

Upd2: But sizeof result is NOT a BYTES ! it is the size of CHAR. And char can be 2 byte, or (may be) 7 bit?

Upd2:但是 sizeof 结果不是 BYTES !它是CHAR的大小。而 char 可以是 2 字节,或者(可能是)7 位?

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.

是的,它是字节。让我再说一遍。 sizeof(char)根据 C 编译器是 1 个字节。人们俗称的字节(8 位)不一定与 C 编译器所称的字节相同。C 字节中的位数因机器架构而异。它也保证至少为 8。

回答by osgx

PDP-10 and PDP-11was.

PDP-10和 PDP-11是。

Update: there like no C99 compilers for PDP-10.

更新:PDP-10 没有 C99 编译器。

Some models of Analog Devices 32-bit SHARC DSP have CHAR_BIT=32, and Texas Instruments DSP from TMS32F28xx have CHAR_BIT=16, reportedly.

据报道,Analog Devices 32 位 SHARC DSP 的某些型号具有 CHAR_BIT=32,TMS32F28xx 的德州仪器 DSP 具有 CHAR_BIT=16 。

Update: There is GCC 3.2 for PDP-10with CHAR_BIT=9 (check include/limits.h in that archive).

更新:PDP-10GCC 3.2,CHAR_BIT=9(检查该档案中的 include/limits.h)。