3 ms·
If a char is to be defined as number from 0 to 255 that and its storage space is exactly 1 byte - that is correct. But it is rather a legacy nowadays. No one s
by c00p3r 16y ago
If a char is to be defined as number from 0 to 255 that and its storage space is exactly 1 byte - that is correct.
But it is rather a legacy nowadays. No one should use it.
- cdavid 16y agoYou missed the explanation: a char is always 1 byte, because in C the definition of byte is char. The wrong assumption is 1 byte == 8 bits.
- c00p3r 16y agoOK, define char. If it is a type built-in C - then of course sizeof(char) = 1. And there is no reason to write such posts. The problem is people are confusing the definition and the usage, because this particular type "char" was supposed to hold a numerical representation of a symbol, which in case of a multi-byte encoding is not 1 byte anymore. So, sizeof(char) is always 1, and what they trying to say is sizeof(numeric-representation-of-a-symbol) which is not a concern of the C language.
- RiderOfGiraffes 16y agoI think you miss the point. A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive. A char is always defined to be the base size of the machine. By definition, therefore, sizeof(char) is always 1. However, a char is not always 1 byte. On some machines a char can hold the values 0..511, and on others 0..65535. (for example)
- masklinn 16y ago> A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive. That definitely isn't true. Historically, bytes went from 5 to 16 bits, it was just the number of bits required to handle a character. If you want to talk specifically about 8-bit bytes in an architecture-independent manner, use the word "octet" (which happens to be the word french people generally use). `char` is a technically independent (though often related) datatype defined as being >= 8 bits by the ANSI standard and the definition of `sizeof(char) == 1` is an axiom of the ANSI standard, not the consequence of anything but itself, though the standard definitely seems to confound (confuse?) chars and bytes: 2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. 3 When 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.88) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
- RiderOfGiraffes 16y agoI stand corrected and apologise - I genuinely mis-spoke (mis-typed?) myself. I do know that a byte is not (historically) necessarily 8-bits - indeed, I regularly used to program machines on which the basic memory unit was 12 bits. The other reply to my comment (http://news.ycombinator.com/item?id=1666345 http://news.ycombinator.com/item?id=1666345) gets this right. Again, apologies.