Date: Fri, 2 May 2003 00:45:53 +0200 From: Erik Trulsson <ertr1013@student.uu.se> To: Bakul Shah <bakul@bitblocks.com> Cc: Dag-Erling Smorgrav <des@ofug.org> Subject: Re: incorrect enum warning? Message-ID: <20030501224552.GA91998@falcon.midgard.homeip.net> In-Reply-To: <200305012231.h41MVePF062201@bitblocks.com> References: <20030501221631.GB546@wombat.fafoe> <200305012231.h41MVePF062201@bitblocks.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, May 01, 2003 at 03:31:40PM -0700, Bakul Shah wrote: > > > 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. > > \begin{nitpicking} > 0x80000000 is *not* an unsigned int, just a regular signed > int but outside the legal range. An unsigned int literal has > to have a U at the end. Since 0x80000000 > INT_MAX, all bets > are off. The result is unspecified and happens to be 1 on a > 2s complement 32 bit machine but can be 0 or anything else on > a ones complement machine or where int is not 32 bits wide. > \end{nitpicking} Your nitpicking is wrong. The type of 0x80000000 (or other hex-constants without any suffix) is the first of int unsigned int long unsigned long long long unsigned long long in which the value of the constant can be represented without overflow. For 32-bit int the first of the types above that can hold the value 2^31 without overflow is unsigned int. -- <Insert your favourite quote here.> Erik Trulsson ertr1013@student.uu.se
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030501224552.GA91998>