问题
I just started to read the ISO C 2011 standard , well the last public draft of it [1] , and realized that in the C Lexical Grammer [1][458ff.] all (literal) numerical constants are unsigned.
Does that mean that the Compiler interpret a signed numerical constant (like -5.1E10 or -1) as a call of the corresponding unary-operator ? e.g -1 <=> -(1) , +512 <=> +(512)
UPDATE: My fault, "all (literal) numerical constants are unsigned" I mean "all (literal) numerical constants are non-negative"
Regard, Thomas
回答1:
All non-suffixed decimal integer literals are signed, but they can not be negative. Perhaps non-negative is what you meant by "unsigned", but I think it's important to make a distinction -- they are not of an unsigned int
type.
Once you've got a positive literal, the unary operator is applied. This is why INT_MIN
is often defined as:
#define INT_MIN (-2147483647 - 1)
Because you can't represent 2147483648 with a signed int
on this platform.
回答2:
Yes, your interpretation is correct, all number literals don't include a sign, an eventual sign is the unary operator applied to it.
The type of a literal is chosen in such a way that the value of the literal is representable within that type, so effectively valid number literals always represent a positive value.
回答3:
all (literal) numerical constants are unsigned.
This is wrong, actually only non-prefixed decimal integer literals are signed. The other integer literals are unsigned or signed.
Does that mean that the Compiler interpret a signed numerical constant (like -5.1E10 or -1) as a call of the corresponding unary-operator ? e.g -1 <=> -(1) , +512 <=> +(512)
If you apply -
to an unsigned literal, its result is (usually) still unsigned.
For example:
-1U // unsigned quantity
-0xFFFFFFFF // unsigned quantity (assuming 32-bit int)
The signed result is converted to unsigned through the rules of C integer conversion.
来源:https://stackoverflow.com/questions/17899318/iso-c-and-signed-literal-constants