Reputation: 313
6.4.4.4/10 ...If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.
I'm having trouble understanding this paragraph. After this paragraph standard gives the example below:
Example 2: Consider implementations that use two’s complement representation for integers and eight bits for objects that have type char. In an implementation in which type char has the same range of values as signed char, the integer character constant '\xFF' has the value −1; if type char has the same range of values as unsigned char, the character constant '\xFF' has the value +255.
What i understand from the expression: "value of an object with type char" is the value we get when we interpret the object's content with type char. But when we look to the example it's like talking about the object's value with pure binary notation. Is my understanding wrong? Does an object's value mean the bits in that object always?
Upvotes: 0
Views: 390
Reputation: 213711
All "integer character constants" (the stuff between '
and '
) have type int
out of tradition and compatibility reasons. But they are mostly meant to be used together with char
, so 6.4.4.4/10 needs to make a distinction between the types. Basically patch up the broken C language - we have cases such as *"\xFF"
that results in type char
but '\xFF'
results in type int
, which is very confusing.
The value '\xFF'
= 255 will always fit in an int
on any implementation, but not necessarily in a char
, which has implementation-defined signedness (another inconsistency in the language). The behavior of the escape sequence should be as if we stored the character constant in a char
, as done in my string literal example *"\xFF"
.
This need for consistency with char
type even though the value is stored in an int
is what 6.4.4.4/10 describes. That is, printf("%d", '\xFF');
should behave just as char ch = 255; printf("%d", (int)ch);
The example is describing one possible implementation, where char
is either signed or unsigned and the system uses 2's complement. Generally the value of an object with integer type refers to decimal notation. char
is an integer type, so it can have a negative decimal value (if the symbol table has a matching index for the value -1 or not is another story). But "raw binary" cannot have a negative value, 1111 1111
can only be said to be -1 if you say the the memory cell should be interpreted as 8 bit 2's complement. That is, if you know that a signed char
is stored there. If you know that an unsigned char
is stored there, then the value is 255.
Upvotes: 3