5 ms·
A char is 1 byte on most systems, but the actual standard defines it as having CHAR_BITS number of bits. Modern C defines CHAR_BITS as CHAR_BITS >= 8. Some olde
by John23832 10y ago
A char is 1 byte on most systems, but the actual standard defines it as having CHAR_BITS number of bits. Modern C defines CHAR_BITS as CHAR_BITS >= 8. Some older machines have 7 bits. Theres nothing stopping machines from changing what CHAR_BITS is.
<Limits.h>
- kps 10y agoPeople are talking past each other. In the C standard ‘byte’ means minimum addressable unit — the unit of sizeof — which need not be exactly 8 bits (but in ANSI C must be at least 8 bits). In C, 'char' is one byte, always and exactly. It's also possible for 'long' to be one byte, provided CHAR_BIT≥32.
- John23832 10y ago> In the C standard ‘byte’ means minimum addressable unit — the unit of sizeof Do you have a reference? I've always seen a byte defined as 8 bits. No matter what. (Unless you're talking to a hard drive manufacturer, that's a different bucket of worms though.)
- kps 10y agoOrginally? See §7.4 of Planning a Computer System: Project Stretch, edited by Werner Buchholz who coined the term. http://archive.computerhistory.org/resources/text/IBM/Stretch/pdfs/Buchholz_102636426.pdf http://archive.computerhistory.org/resources/text/IBM/Stretc... For C, see C11 §3.6: “byte — addressable unit of data storage large enough to hold any member of the basic character set of the execution environment” http://www.iso-9899.info/n1570.html#3.6 http://www.iso-9899.info/n1570.html#3.6
- John23832 10y agoMakes sense now. Thanks
- jwilk 10y ago(From the C99 standard:) § 6.5.3.4 The sizeof operator […] Semantics 2 The sizeof operator yields the size (in bytes) of its operand, […] 3 When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
- Someone 10y agoThe draft ISO/IEC 9899:201x PDF of "Programming Languages — C" says: "3.5 bit unit of data storage in the execution environment large enough to hold an object that may have one of two values NOTE It need not be possible to express the address of each individual bit of an object. 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." I can't find any indication that a byte is the minimum addressable unit. I guess that is because of 4-bit CPUs and/or CPUs that can address individual bits. It also uses "byte" very sparingly, most of the time in the context of multibyte (not hyphenated) to single-byte (hyphenated) conversions, but also in section 5.2.1: "The representation of each member of the source and execution basic character sets shall fit in a byte." And there also is a guarantee that a unsigned char is as large as a byte in a footnote to section 6.2.6.1: "A byte contains CHAR_BIT bits, and the values of type unsigned char range from 0 to 2^CHAR_BIT − 1" I doubt that char and unsinged char can be of different size, but didn't check that.