I
Ioannis Vranos
Under C95:
Is it guaranteed that char, unsigned char, signed char have no padding bits?
Is it guaranteed that char, unsigned char, signed char have no padding bits?
Ioannis said:Under C95: Is it guaranteed that char, unsigned char, signed
char have no padding bits?
unsigned char, yes.
The others by implication.
Harald said:Just a note: padding bits are a concept introduced in the standard in
C99; C90/C95 left much more unspecified about the representation of
integer types.
Where is this guarantee made?
In C99, 5.2.4.2.1 makes it as clear as it
can: "The value UCHAR_MAX shall equal 2^CHAR_BIT - 1." I don't have a
copy of an older standard. Does it make the same guarantee?
How so? What's preventing a signed integer type and its corresponding
unsigned type from having a different number of padding bits?
Harald van Dijk said:Just a note: padding bits are a concept introduced in the standard in
C99; C90/C95 left much more unspecified about the representation of
integer types.
Where is this guarantee made? In C99, 5.2.4.2.1 makes it as clear as it
can: "The value UCHAR_MAX shall equal 2^CHAR_BIT - 1."
Ben Bacarisse said:But that alone is not enough, is it?
The clearest statement comes
later in 6.2.6. p1: "For unsigned integer types other than unsigned
char, the bits of the object representation shall be divided into two
groups: value bits and padding bits (there need not be any of the
latter).". So, unsigned char has only value bits.
Keith Thompson said:I believe it is. Assume, for example, CHAR_BIT==8. Then there are
256 possible values for the 8 bits making up an unsigned char.
UCHAR_MAX must equal 255, so there are 256 distinct values, each of
which must be distinctly representable by one of those bit patterns.
There are no bit patterns left over, so there can't be any padding
bits or trap representations. This generalizes to other values of
CHAR_BIT.
You mean 6.2.6.2p1.
This describes types other than unsigned char; it says nothing about
unsigned char itself. If unsigned char were composed entirely of
naughty bits, with no value bits or padding bits at all, it would not
contradict that sentence (though it would contradict other
requirements).
In fact, the phrase "other than unsigned char" could have been left
out. The bits of the object representation of *any* unsigned type are
divided into value bits and padding bits, and there needn't be any
padding bits. Furthermore, for unsigned char there *must* not be any
padding bits (as implied by 5.2.4.2.1).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.