0

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

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 2
    A byte is *never* guaranteed to be exactly 8 bits - it's just the most common architecture – UnholySheep Jan 20 '19 at 10:23
  • 1
    `sizeof(char)` is 1, so `sizeof(foo) / sizeof(char)` is `sizeof(foo)`, which is `5 * sizeof(char)`, which is 5 regardless of how many bits a `char` has. – molbdnilo Jan 20 '19 at 10:32
  • If the expression `e` has type `T`, then `sizeof(e)` is equivalent to `sizeof(T)`. – molbdnilo Jan 20 '19 at 10:38
  • Yes I know that but forgot about the 3.6 point mentioned in @StroryTeller answer – 0___________ Jan 20 '19 at 10:49
  • See also: https://stackoverflow.com/questions/8107133/does-recv-work-with-bytes-or-octets-or-are-they-one-and-the-same-in-the-conte https://stackoverflow.com/questions/5516044/system-where-1-byte-8-bit https://stackoverflow.com/questions/14421101/can-the-size-of-the-byte-greater-than-octet-8-bits – ninjalj Jan 20 '19 at 12:26

1 Answers1

3

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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458