0___________
0___________

Reputation: 67476

What is the byte?

C standard uses the word byte in many different places. Mostly it is something very similar to my understanding of this word - 8 bits long chunk of data.

But :

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

And:

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

Later:

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

So if we consider the machine with char having more than 8 bits the observable behavior of this program will be differ from the 8bits char machine.

char foo[5];

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

So maybe the byte meaning is different in the C standard understanding. Could anyone explain: is byte 8 bits or byte is something different

And one extra question.

is sizeof(char) == sizeof(array[0])? Considering the byte size differences

Upvotes: 0

Views: 341

Answers (1)

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.

This is a byte according to the C standard. Its minimum size is just the amount of bits required to hold the basic character set of the execution environment, i.e. a minimum of 8 nowadays IIRC. The exact size of a byte in bits is encoded in the CHAR_BIT macro.

Upvotes: 4

Related Questions