Are there machines (or compilers), where sizeof(char) != 1
?
Does C99 standard says that sizeof(char)
on standard compliance implementation MUST be exactly 1? If it does, please, give me section number and citation.
Update:
If I have a machine (CPU), which can't address bytes (minimal read is 4 bytes, aligned), but only 4-s of bytes (uint32_t
), can compiler for this machine define sizeof(char)
to 4?sizeof(char)
will be 1, but char will have 32 bits (CHAR_BIT
macros)
Update2: But sizeof result is NOT a BYTES ! it is the size of CHAR. And char can be 2 byte, or (may be) 7 bit?
Update3:
Ok. All machines have sizeof(char) == 1
. But what machines have CHAR_BIT > 8
?
It is always one in C99, section 6.5.3.4:
Edit: not part of your question, but for interest from Harbison and Steele, 3rd ed. (pre c99) p. 148:
Edit: In answer to your updated question, the following question and answer from Harbison and Steele is relevant (ibid, Ex. 4 of Ch. 6):
Answer (ibid, p. 382):
While this does not specifically address a case where, say bytes are 8 bits and
char
are 4 of those bytes (actually impossible with the c99 definition, see below), the fact thatsizeof(char) = 1
always is clear from the c99 standard and Harbison and Steele.Edit: In fact (this is in response to your upd 2 question), as far as c99 is concerned
sizeof(char)
is in bytes, from section 6.5.3.4 again:so combined with the quotation above, bytes of 8 bits and
char
as 4 of those bytes is impossible: for c99 a byte is the same as achar
.In answer to your mention of the possibility of a 7 bit
char
: this is not possible in c99. According to section 5.2.4.2.1 of the standard the minimum is 8:Their implementation-defined values shall be equal or greater [my emphasis] in magnitude to those shown, with the same sign.
— number of bits for smallest object that is not a bit-field (byte)
— minimum value for an object of type signed char
— maximum value for an object of type signed char
— maximum value for an object of type unsigned char
— minimum value for an object of type char
— maximum value for an object of type char
[...]
PDP-10
and PDP-11was.Update:
there like no C99 compilers for PDP-10.Some models of Analog Devices 32-bit SHARC DSP have CHAR_BIT=32, and Texas Instruments DSP from TMS32F28xx have CHAR_BIT=16, reportedly.
Update: There is GCC 3.2 for PDP-10 with CHAR_BIT=9 (check include/limits.h in that archive).
There are no machines where
sizeof(char)
is 4. It's always 1 byte. That byte might contain 32 bits, but as far as the C compiler is concerned, it's one byte. For more details, I'm actually going to point you at the C++ FAQ 26.6. That link covers it pretty well and I'm fairly certain C++ got all of those rules from C. You can also look at comp.lang.c FAQ 8.10 for characters larger than 8 bits.Yes, it is bytes. Let me say it again.
sizeof(char)
is 1 byte according to the C compiler. What people colloquially call a byte (8 bits) is not necessarily the same as what the C compiler calls a byte. The number of bits in a C byte varies depending on your machine architecture. It's also guaranteed to be at least 8.