是否有机器,sizeof(char) != 1,或至少 CHAR_BIT >8? [英] Are there machines, where sizeof(char) != 1, or at least CHAR_BIT > 8?

查看:21
本文介绍了是否有机器,sizeof(char) != 1,或至少 CHAR_BIT >8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有机器(或编译器),其中 sizeof(char) != 1?

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

C99 标准 是否规定标准合规实施上的 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.

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

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)

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

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?

更新3:行.所有机器都有 sizeof(char) == 1.但是什么机器有 CHAR_BIT >8 ?

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

推荐答案

在C99 6.5.3.4 中总是为1:

It is always one in C99, section 6.5.3.4:

当应用于具有输入 char、unsigned char 或 signedchar,(或其合格版本)结果是 1.

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

不是您问题的一部分,而是出于哈比森和斯蒂尔的.C:参考手册,第三版,Prentice Hall,1991 年(c99 之前)p.148:

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:

一个存储单元被认为是一个占用的存储量特点;对象的大小因此类型 char 为 1.

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.

在回答您更新的问题时,Harbison 和 Steele 的以下问答是相关的(同上,Ex. 4 of Ch. 6):

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

是否允许有一个Cchar 类型的实现表示范围从-2,147,483,648 到 2,147,483,647?如果是这样,什么是 sizeof(char)在那个实施下?什么会是最小和最大范围输入 int?

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?

答案(同上,第 382 页):

Answer (ibid, p. 382):

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

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.

虽然这并没有具体说明字节是 8 位而 char 是其中 4 个字节的情况(实际上用 c99 定义是不可能的,见下文),但事实上 sizeof(char) = 1 在 c99 标准以及 Harbison 和 Steele 中总是很清楚.

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.

事实上(这是对您的 upd 2 问题的回应),就 c99 而言 sizeof(char) is 以字节为单位,来自第 6.5 节.3.4 再次:

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:

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

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

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

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.

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

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//−(27−1)** 

—signed char 类型对象的最大值

— maximum value for an object of type signed char

**SCHAR_MAX +127//27−1** 

— unsigned char 类型对象的最大值

— maximum value for an object of type unsigned char

**UCHAR_MAX 255//28−1** 

——char 类型对象的最小值

— minimum value for an object of type char

**CHAR_MIN**    see below 

——char 类型对象的最大值

— maximum value for an object of type char

**CHAR_MAX**    see below

[...]

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

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.

这篇关于是否有机器,sizeof(char) != 1,或至少 CHAR_BIT >8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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