Skip to contentSkip to conversation

decimal constant so large?

DISCUSSION [email protected] 3 participants 3 messages May 28, 2005 – May 31, 2005

lore
Share & tools
lore
#0 lore raw

Hi everybody,

Can anyone tell me why the constant -2147483648, according to what g++ reports as a warning for the code below, is considered to be "so large that it is unsigned"?

How can it be unsigned when it starts with '-'?

I am compiling on an x86, and the constant seems to be a perfectly good 32 bit signed value to me. Where am I wrong?

I've tried it with three different compilers, and they all behave in the same funny way: Borland C++ compiler 5.7, as well as g++ 3.2.3 report a warning, Intel compiler 8.1 says nothing.

==============================================================================
#include <iostream>

using namespace std;

int main() {

cout << (int)-2147483648 << endl; // ok
cout << -2147483648 << endl; // prints 2147483648 instead

}
==============================================================================

#1 lore raw
Quoted 1 line · [email protected] wrote:

Can anyone tell me why the constant -2147483648,

-2147483648 isn't a constant, it's an expression consisting of the negation operator "-" applied to the decimal constant "2147483648".

The decimal constant 2147483648 is too large to fit into a signed 32-bit integer, so it is treated as unsigned.

If you want the most negative signed 32-bit integer, you have to write it as (-2147483647 - 1).

Signature · 1 line
Glynn Clements <[email protected]>
#2 lore raw

Training

I am looking for a training establishment in the South East (United States) that offers beginners to advanced training in C with a bang for the buck.

Anyone have any suggestions?

Thanks,

- Darren