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

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

问题描述

是否有机器(或编译器),其中的sizeof(字符)!= 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,但字符将有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)

UPDATE2:
不过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?

UPDATE3:
好。所有机器都有的sizeof(char)的== 1 。但是,什么机器有 CHAR_BIT&GT; 8

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

推荐答案

它总是之一,C99,节6.5.3.4:

It is always one in C99, section 6.5.3.4:

当施加到具有一个操作数
  char型,unsigned char型或签名
  CHAR(或其合格的版本)
  结果为1

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

编辑:不是你的问题的一部分,但是从哈比森和Steele,第三版的兴趣。 (pre C99)页。 148:

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

一个存储单元被取为
  存储量由一个占据
  字符;的一个对象的大小
  因此键入字符 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.

编辑:在回答你的问题的更新,从哈比森和Steele以下问题和答案是相关的(同上,防爆的CH 4 6。):

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

是不是可容许有一个C
  在这种类型的实施字符 CAN
  再present值从
  通过-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位
  再present键入字符。不管
  实施的值
  的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位的情况下字符是那些字节4(与C99定义实际上是不可能的,见下文)事实上,的sizeof(char)的= 1 总是从C99标准和哈比森和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)的的以字节为单位,从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位的字节,并且的那些字节4是不可能的:对C99一个字节是一样的字符

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位的可能性提字符:这是不可能的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类型的对象的值
  被视为一个有符号整数时,
  在离pression使用的价值
  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(字符)= 1,或至少CHAR_BIT&GT!; 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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