什么是字节?

What is the byte?

C 标准在许多不同的地方使用字节这个词。大多数情况下,它与我对这个词的理解非常相似——8 位长的数据块。

但是:

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

并且:

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

之后:

When applied to an operand that has array type, the result is the total number of bytes in the array.

因此,如果我们考虑 char 的机器具有超过 8 位,则该程序的可观察行为将不同于 8 位字符机器。

char foo[5];

for(size_t index = 0; index < sizeof(foo) / sizeof(char); index++)
{
    /* some code */
}

所以可能字节含义在C标准理解上是不同的。谁能解释一下:字节是 8 位还是字节不同

还有一题。

sizeof(char) == sizeof(array[0])?考虑字节大小差异

3. Terms, definitions, and symbols

3.6 byte addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

NOTE 1 It is possible to express the address of each individual byte of an object uniquely.

NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementation- defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit.

根据 C 标准,这是一个字节。它的最小大小只是保存执行环境的基本字符集所需的位数,即现在 IIRC 最少 8 位。以位为单位的字节的确切大小在 CHAR_BIT 宏中编码。