是否存在 sizeof(char) != 1 或至少 CHAR_BIT > 的机器8?

发布于 2024-08-20 16:44:36 字数 568 浏览 13 评论 0原文

是否存在 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 技术交流群。

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

发布评论

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

评论(3

左耳近心 2024-08-27 16:44:36

在 C99 中,它始终为 1,第 6.5.3.4 节:

当应用于具有
类型 charunsigned charsigned char(或其限定版本)
结果是1

编辑:不是您问题的一部分,而是出于 哈比森和斯蒂尔的兴趣。 C:参考手册,第三版,Prentice Hall,1991(c99 之前)第 14 页。 148:

存储单元被视为
一个占用的存储量
特点;物体的大小
因此,char 类型为 1

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

可以有C吗
类型 char 可以实现
代表值范围为
-2,147,483,648 到 2,147,483,647?如果是这样,sizeof(char) 会是多少
根据该实施?什么会
是最小和最大范围
类型int

答案(同上,第 382 页):

允许(如果浪费的话)
使用 32 位实现
表示类型 char。不管
实施、价值
sizeof(char) 始终为 1。

虽然这并没有具体解决这样的情况:假设字节是 8 位,而 char 是其中的 4 个字节(对于 c99 实际上是不可能的)定义,见下文),事实上 sizeof(char) = 1 总是从 c99 标准以及 Harbison 和 Steele 中清楚地看出。

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

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

与上面的引用相结合,8位字节和char作为其中4个字节是不可能的:对于c99,字节与char相同代码>.

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

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

—非位字段(字节)的最小对象的位数

CHAR_BIT 8

——signed char类型对象的最小值

SCHAR_MIN -127

——signed char类型对象的最大值

SCHAR_MAX +127

——unsigned char类型对象的最大值

UCHAR_MAX 255

——char类型对象的最小值

CHAR_MIN见下文

——char类型对象的最大值

CHAR_MAX见下文

[...]

如果char类型的对象的值
被视为有符号整数时
在表达式中使用时,值
CHAR_MIN 应与
SCHAR_MINCHAR_MAX 的值
应与
SCHAR_MAX。否则,值
CHAR_MIN 应为 0 并且值
CHAR_MAX 应与
UCHAR_MAX。值UCHAR_MAX
应等于2CHAR_BIT - 1

It is always one in C99, section 6.5.3.4:

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

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:

A storage unit is taken to be the
amount of storage occupied by one
character; the size of an object of
type char is therefore 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):

Is it allowable to have a C
implementation in which type char can
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?

Answer (ibid, p. 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.

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 that sizeof(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:

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

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 a 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 minimum is 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

— maximum value for an object of type signed char

SCHAR_MAX +127

— maximum value for an object of type unsigned char

UCHAR_MAX 255

— minimum value for an object of type char

CHAR_MIN see below

— maximum value for an object of type 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 2CHAR_BIT − 1.

江心雾 2024-08-27 16:44:36

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

Upd2:但 sizeof 结果不是字节
!它是 CHAR 的大小。和炭可以
是 2 字节,还是(可能是)7 位?

是的,是字节。我再说一遍。根据 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.

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?

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.

心病无药医 2024-08-27 16:44:36

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-11 was.

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

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