Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 2 May 2003 00:16:32 +0200
From:      Stefan Farfeleder <stefan@fafoe.dyndns.org>
To:        Mike Hunter <mhunter@ack.Berkeley.EDU>
Cc:        Dag-Erling Smorgrav <des@ofug.org>
Subject:   Re: incorrect enum warning?
Message-ID:  <20030501221631.GB546@wombat.fafoe>
In-Reply-To: <20030501214326.GA10277@ack.Berkeley.EDU>
References:  <xzp7k9a67pf.fsf@flood.ping.uio.no> <20030501150713.GA34992@madman.celabo.org> <20030501152022.GC568@wombat.fafoe> <xzpr87ipefn.fsf@flood.ping.uio.no> <20030501213418.GA42794@falcon.midgard.homeip.net> <20030501214326.GA10277@ack.Berkeley.EDU>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, May 01, 2003 at 02:43:26PM -0700, Mike Hunter wrote:
> On May 01, "Erik Trulsson" wrote:
> 
> > On Thu, May 01, 2003 at 11:03:40PM +0200, Dag-Erling Smorgrav wrote:
> > > Stefan Farfeleder <stefan@fafoe.dyndns.org> writes:
> > > > Because 0x80000000 > INT_MAX on 32-Bit architectures, 0x80000000 has
> > > > type unsigned.  But enumeration constants always have type int, that's
> > > > why you're getting this warning.
> > > 
> > > but 0x80000000 == INT_MIN on 32-bit two's complement systems...
> > 
> > No. 0x80000000 has type unsigned int (assuming 32-bit int) and is thus
> > a large positive number.  INT_MIN has type signed int and is a negative
> > number.  The fact that they happen to have the same representation does
> > not mean they are the same thing.
> 
> #include <stdio.h>
> #include <limits.h>
> 
> int main ()
> {
>         printf("%d\n", (0x80000000 == INT_MIN));
>         return 0;
> }
> 
> ./a.out
> 1
> 
> Just pointing out that they do "==" each other, which is what was said.

That is because the comparison is done in unsigned arithmetics due to
the "usual arithmetic conversions".  The negative int value INT_MIN is
converted to unsigned int by adding UINT_MAX + 1 before the comparison
is evaluated.

Regards,
Stefan Farfeleder



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030501221631.GB546>