cout << -2147483648 ; strange output in g++

J

James Kanze

No diagnostic is required. The behavior is UNDEFINED.

Not according to §2.13.1/3, in C++98, C++03 and the current
draft. Both C++98 and C++03 contradict themselves, and say that
it is undefined elsewhere; this has been corrected in the
current draft.
I doubt seriously his compiler makes it unsigned.

If his compiler is g++, it makes it unsigned long.
I've never seen one that does it.

It's what C90 requires, and what g++ (and probably many other
compilers) actually do.
Most do, as the OP's does, end up with a int or long int
number that doesn't actually represent the literal expressed.

I've never seen a compiler that didn't complain if it actually
had an overflow.
 
J

James Kanze

You're correct. I didn't read the next paragraph, [lex.icon]/3,
which states that the code is ill-formed if the literal cannot
be represented.

And you're correct, according to the preceding paragraph, in
C++98 and C++-03. It does happen that the standard contradicts
itself, but in this case, it does so in less than 10 lines. (I
suspect the reason is that the wording in paragraph 2 was taken
verbatum from a late draft of C99, and no one noticed that in
doing so, they accidentally introduced words to make the
behavior undefined, when they'd intentionally required a
diagnostic previously. But that's really just a guess on my
part.)

As far as I know, there was never a defect report about this
either. It's corrected in the current draft, but again, that's
perhaps just because the text taken verbatim from C99 didn't
explicitly say undefined behavior, so there is nothing which
contradicts the final statement.

Anyhow, I posted a summary of all the information I happen to
have available, and given the evolution, it's not surprising
that people (and compilers) are confused.
 
J

James Kanze

Hiya James!
Just thought I'd mention it, since Rolf pointed out Suresh
quoted your signature: Your sig is missing a blankspace after
the initial two dashes - newsreaders won't recognize this as a
signature so they could auto-remove it upon responding.

We've been through this before:). The blank is there when I
enter the text into the post box in Google News. After that...

From what I have been able to determine: when I post from home,
the blank remains; when I post from work, it seems to be
stripped out somewhere. In both cases, I'm using the latest
version of Firefox, on a machine running Linux kernel 2.6. The
main difference I can see is that at work, the posting goes
through a firewall (over which I have no influence); it's just a
guess, but I suspect that the firewall is the problem.
 
L

Lars Uffmann

James said:
We've been through this before:). The blank is there when I
enter the text into the post box in Google News. After that...
Ah okay - didn't realize you were aware of this - nor did I check your
header to see you were using google groups :)

Have a nice day!

Lars
 
S

suresh

Thanks Rolf Magnus, so this means that the g++ compiler takes
-2147483648 as unsigned long?
 
I

Ian Collins

suresh wrote:

[you just top posted again]
Thanks Rolf Magnus, so this means that the g++ compiler takes
-2147483648 as unsigned long?
It interprets 2147483648 as an unsigned long (read James' post again).

On my system, the result is "long long"...
 
R

Rolf Magnus

Again, please don't top-post.
Thanks Rolf Magnus, so this means that the g++ compiler takes
-2147483648 as unsigned long?

Yes. It takes 2147483648 as unsigned long, then applies the unary '-'
operator to it, and the result of that is of the same type.
 
J

James Kanze

Yes. It takes 2147483648 as unsigned long, then applies the unary '-'
operator to it, and the result of that is of the same type.

What's annoying, of course, is that it does this even if you've
used the options for it to support long long. I'd call that a
bug. (If the compiler doesn't support long long, the behavior
is perfectly acceptable, because it does output a diagnostic, as
required by the standard.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top