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

查看:24
本文介绍了是否有机器,其中 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 个字节(uint32_t),可以为此编译machinedefine sizeof(char) to 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 结果不是 BYTES !它是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 中始终为 1,第 6.5.3.4 节:

It is always one in C99, section 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.

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

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

一个存储单元被认为是一个占用的存储量特点;物体的大小type 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 的以下问题和答案是相关的(同上,第 6 章的第 4 部分):

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

——符号类型对象的最大值

— 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天全站免登陆