Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > INT_MIN and compiler diagnostic

Reply
Thread Tools

INT_MIN and compiler diagnostic

 
 
p_cricket_guy@yahoo.co.in
Guest
Posts: n/a
 
      02-28-2007

Please see this test program:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4
5 int main (void)
6 {
7 int y = -2147483648;
8 int x = INT_MIN;
9
10 printf("INT_MAX = %d INT_MIN = %d\n", INT_MAX,
INT_MIN);
11 printf("x = %d y = %d\n", x, y);
12
13
14 return EXIT_SUCCESS;
15 }

Output:
INT_MAX = 2147483647 INT_MIN = -2147483648
x = -2147483648 y = -2147483648

When I compile this using gcc, I get a diagnostic on line 7:

[pcg@mylinux test]$gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
/tmp/x.c: In function `main':
/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90

However, on my system INT_MIN is indeed -2147483648 as
suggested by the output of above program.

Is there any specific reason for this diagnostic [as per ANSI C] ?

However, line 8, which is logically equivalent to line 7 does not
produce any diagnostic. INT_MIN is defined in limits.h as:

# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647

Thanks,
pcg

 
Reply With Quote
 
 
 
 
bytebro
Guest
Posts: n/a
 
      02-28-2007
On 28 Feb, 10:07, p_cricket_...@yahoo.co.in wrote:
> When I compile this using gcc, I get a diagnostic on line 7:
>
> [pcg@mylinux test]$gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
> /tmp/x.c: In function `main':
> /tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90


On mine (Sun sparc) using gcc with exactly the same flags I get

x.c:7: warning: decimal constant is so large that it is unsigned

The program output is:

INT_MAX = 2147483647 INT_MIN = -2147483648
x = -2147483648 y = -2147483648

So amusingly, it tells me the constant is unsigned, then prints it
signed.

Where's a language lawyer when you need one?!

 
Reply With Quote
 
 
 
 
Beej Jorgensen
Guest
Posts: n/a
 
      02-28-2007
<> wrote:
>/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90


I think it was addressed in this thread:

http://tinyurl.com/ytuxdv

># define INT_MIN (-INT_MAX - 1)
># define INT_MAX 2147483647


This produces the warning:

int y = -2147483648;

whereas this does not:

int y = (-2147483647 - 1);

(Interestingly, though, I guess I expected the preprocessor to calculate
the final answer before passing it to the compiler, and it doesn't.

And now, it doesn't even make sense that the preprocessor would do it.
I mean, the compiler has to do that kind of stuff anyway, right?

So why did I have it in the back of my mind that the preprocessor
sometimes did simple math? Some holdover from the olden days of not-so-
optimal compilers?)

Finally,

int y = -0x80000000;

does not produce a warning, though it is the same as -2147483648.
Apparently the rules are different for hex constants than they are for
decimal constants (c99 6.4.4.1p5).

-Beej

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-28-2007
bytebro wrote:

> INT_MAX = 2147483647 INT_MIN = -2147483648
> x = -2147483648 y = -2147483648
>
> So amusingly, it tells me the constant is unsigned, then prints it
> signed.
>
> Where's a language lawyer when you need one?!


If INT_MAX equals 2147483647,
the the type of 2147483648 can't be type int, can it?

--
pete
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      02-28-2007
bytebro wrote:

> On 28 Feb, 12:34, pete <pfil...@mindspring.com> wrote:
>> bytebro wrote:
>> > INT_MAX = 2147483647 INT_MIN = -2147483648
>> > x = -2147483648 y = -2147483648

>>
>> > So amusingly, it tells me the constant is unsigned, then prints it
>> > signed.

>>
>> > Where's a language lawyer when you need one?!

>>
>> If INT_MAX equals 2147483647,
>> the the type of 2147483648 can't be type int, can it?

>
> Erm... it doesn't say "2147483648", it says "-2147483648", which is
> exactly equal to INT_MIN, and is therefore a valid int value. The
> warning is therefore misleading, no?


No.

`-2147483648` isn't a literal constant. It's an expression, the
negation of `2147483648`.

--
Chris "electric hedgehog" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

 
Reply With Quote
 
bytebro
Guest
Posts: n/a
 
      02-28-2007
On 28 Feb, 12:34, pete <pfil...@mindspring.com> wrote:
> bytebro wrote:
> > INT_MAX = 2147483647 INT_MIN = -2147483648
> > x = -2147483648 y = -2147483648

>
> > So amusingly, it tells me the constant is unsigned, then prints it
> > signed.

>
> > Where's a language lawyer when you need one?!

>
> If INT_MAX equals 2147483647,
> the the type of 2147483648 can't be type int, can it?


Erm... it doesn't say "2147483648", it says "-2147483648", which is
exactly equal to INT_MIN, and is therefore a valid int value. The
warning is therefore misleading, no?


 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      02-28-2007
In article < om> "bytebro" <> writes:
> On 28 Feb, 12:34, pete <pfil...@mindspring.com> wrote:
> > bytebro wrote:
> > > INT_MAX = 2147483647 INT_MIN = -2147483648
> > > x = -2147483648 y = -2147483648

> >
> > > So amusingly, it tells me the constant is unsigned, then prints it
> > > signed.

> >
> > > Where's a language lawyer when you need one?!

> >
> > If INT_MAX equals 2147483647,
> > the the type of 2147483648 can't be type int, can it?

>
> Erm... it doesn't say "2147483648", it says "-2147483648", which is
> exactly equal to INT_MIN, and is therefore a valid int value. The
> warning is therefore misleading, no?


Does it tell you "-2147483648" is unsigned? Strange. It should
tell you the constant is unsigned, and the constant is "2147483648".
There are no negative constants in C.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-28-2007
Beej Jorgensen wrote:
>

.... snip ...
>
> This produces the warning:
>
> int y = -2147483648;
>
> whereas this does not:
>
> int y = (-2147483647 - 1);
>

.... snip ...
>
> Finally,
>
> int y = -0x80000000;
>
> does not produce a warning, though it is the same as -2147483648.
> Apparently the rules are different for hex constants than they are
> for decimal constants (c99 6.4.4.1p5).


In the first case you are negating the int 2147483648, which has
already overflowed and caused un/implementation defined behaviour.
In the second, you are negating the unsigned int 0x80000000, which
follows the rules for unsigned ints, and does not overflow.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-28-2007
Dik T. Winter said:

<snip>

> There are no negative constants in C.


"No" is a counter-example.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
bytebro
Guest
Posts: n/a
 
      02-28-2007
On 28 Feb, 13:20, Chris Dollin <chris.dol...@hp.com> wrote:
> bytebro wrote:
> > On 28 Feb, 12:34, pete <pfil...@mindspring.com> wrote:

>
> >> If INT_MAX equals 2147483647,
> >> the the type of 2147483648 can't be type int, can it?

>
> > Erm... it doesn't say "2147483648", it says "-2147483648", which is
> > exactly equal to INT_MIN, and is therefore a valid int value. The
> > warning is therefore misleading, no?

>
> No.
>
> `-2147483648` isn't a literal constant. It's an expression, the
> negation of `2147483648`.


Wow. I've been mucking around with C for about 20 years now, and
that's the first time I've come across that!

The thing is, in the OP's code lines 7 and 8, it says:

7 int y = -2147483648;
8 int x = INT_MIN;

which are _completely_ equivalent (INT_MIN is _defined_ as
-214748364, and yet line 7 generates the warning:

/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO
C90

on his system, and the warning:

x.c:7: warning: decimal constant is so large that it is unsigned

on my system, but line 8 generates no warning at all for either of us.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(INT_MIN -1) > 0 equals to 0?? Andy C Programming 8 03-18-2009 11:51 PM
(unsigned)-INT_MIN viza C Programming 22 07-26-2008 01:53 AM
negate INT_MIN jzhang918@gmail.com C Programming 2 02-10-2007 09:15 AM
INT_MIN as decimal Jordan Abel C Programming 15 03-09-2006 01:40 AM
C99: Is "INT_MIN % -1" well defined? M Welinder C Programming 6 08-06-2004 11:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57