3

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

Thomas T.
  • 65
  • 1
  • 1
  • 5

3 Answers3

4

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.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • 4
    No, not all literals are signed, you probably mean "all decimal literals without `U` suffix are signed". Octal and hexadecimal literals can be unsigned, even without an `U` suffix. – Jens Gustedt Jul 27 '13 at 15:26
3

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

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.

ouah
  • 142,963
  • 15
  • 272
  • 331