Mark McIntyre said:
On 3 Apr 2005 16:09:49 -0700, in comp.lang.c ,
What exactly is "it"? What is "split as an operator and positive number"?
A value stored in a variable is not split in this way; it's just a
value. I think what you're referring to is how to represent the value
as a C expression.
Okay, I see what it means. Yes, in that sense it is treated thus.
There's no way to stop it, this is how the C parser works, the "-" is
a unary operator and operates on its operand, in this case 2147483648.
Since that won't fit into an int, you can't use it.
But now we're back to what I said - you're trying to fit a quart into
a pint pot. Choose a variable thats the right size.
In the following I'll assume that "int" is a 32-bit 2's-complement
type with no padding bits or trap repsentations. When I use the
phrase "numeric value", I'm talking about a mathematical integer
value, not a C expression or a value of any C type. Finally, I'll use
the term "integer literal" where the standard talks about "integer
constants", just to avoid confusion with the unrelated "const"
feature.
I think the point is that the OP wants to store the numeric value
-2147483648 in an int. Since this happens to be the value of INT_MIN,
it should be possible. Unfortunately, it's not as easy as you might
expect.
C has no negative integer literals, only non-negative ones. Something
like -42 is not a literal, it's a literal with a unary "-" operator
applied to it. In many contexts, this difference doesn't matter, but
you've found one where it does.
If -2147483648 were treated as a negative literal, then this:
int a = -2147483648;
would be ok. Instead, 2147483648 is a literal, and it can't be
represented as an int. Let's assume type "long int" is 64 bits,
making 2147483648 a literal of type long int with the obvious value.
Applying the "-" to this value yields a long int with the numeric
value -2147483648. Using this to initialize an int causes an implicit
conversion, and stores the expected value in a.
But in a C90 implementation, the longest integer might be only 32
bits. This means that 2147483648 is not a value of *any* integer
type. Attempting to use it in a program results in a warning.
The declaration
int a = -2147483648;
will still *probably* do what you expect, but there's no guarantee,
and you can expect the compiler to warn you that you're doing
something questionable.
An easy workaround is to use a slightly more complicated expression:
int a = -2147483647-1;
Both literals (with positive values) are within the range of type int,
and none of the intermediate expressions cause an overflow.
Out of the 4294967296 distinct values of a 32-bit 2's-complement
integer type, 42949672965 can be easily represented in C by either a
single integer literal, or by an integer literal with a unary "-"
operator; only one value cannot be represented this way. This is
arguably a hole in the language, but given that there's an easy
workaround, it's probably not worth adding a special-case feature to
correct it.
Some more things to keep in mind:
The standard doesn't guarantee that int is 32 bits; the minimum
guaranteed range is only -32767 .. +32767.
If you're assuming a 32-bit 2's-complement representation for int, you
can just use INT_MIN, defined in <limits.h>.