From owner-freebsd-questions Mon Nov 13 0:30:21 2000 Delivered-To: freebsd-questions@freebsd.org Received: from christel.heitec.net (christel.heitec.net [193.101.232.3]) by hub.freebsd.org (Postfix) with ESMTP id A3C2A37B4C5 for ; Mon, 13 Nov 2000 00:30:17 -0800 (PST) Received: from heitec.net (paladin.heitec.net [193.101.232.30]) by christel.heitec.net (Postfix) with ESMTP id EACE235484A; Mon, 13 Nov 2000 09:36:10 +0100 (CET) Message-ID: <3A0FA6D7.38C88CF7@heitec.net> Date: Mon, 13 Nov 2000 09:31:19 +0100 From: bdluevel@heitec.net X-Mailer: Mozilla 4.04 [en] (WinNT; I) MIME-Version: 1.0 To: cjclark@alum.mit.edu, Salvo Bartolotta , freebsd-questions@FreeBSD.ORG Subject: Re: OT: curious (??) integer types behavio(u)r... References: <20001112.21033200@bartequi.ottodomain.org> <20001112143032.Q75251@149.211.6.64.reflexcom.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Crist J . Clark wrote: > > On Sun, Nov 12, 2000 at 09:03:32PM +0000, Salvo Bartolotta wrote: [...] > >
> > /* A simple test program */ > > > > #include > > > > main() > > { > > char c1 = -128; > > char c2 = 127; > > unsigned char c = 255; > > > > short int si1 = -32768; > > short int si2 = 32767; > > unsigned short int si = 65535; > > > > int d1 = -2147483648; > > int d2 = 2147483647; > > unsigned int d = 4294967295; > > unsigned long int l = 4294967295; /* redundant */ > > > > printf("%d %d %d %d %d %d %d %d %d %ld\n", c1, c2, c, si1, > > si2, si, d1, d2, d, l); > > } > > > >
> > > > >====> cc -o mytest mytest.c > > mytest.c: In function `main': > > mytest.c:15: warning: decimal constant is so large that it is unsigned > > mytest.c:17: warning: decimal constant is so large that it is unsigned > > mytest.c:18: warning: decimal constant is so large that it is unsigned > > > > > > >====> ./mytest > > -128 127 255 -32768 32767 65535 -2147483648 2147483647 -1 -1 > > (?) ^^^^^^ > > > > > > > > To sum up: > > 1) cc complains about line 15, but the limit is correctly displayed by > > the program (!); > > Yeah, that is strange. No, it's right. The line in question is int d1 = -2147483648; But here, the number 2147483648 can't be represented by a signed 'int'; however, there is no L or U suffix so the programmer may well intend such an 'int'. GCC correctly warns that an automatic conversion will be applied, resulting in 2147483648U. Note that the '-' sign doesn't help, it's not part of the number but an operator that is applied to the number. So what's written is: Take the 'int' value 2147483648, then negate it, then store the result in the freshly created int variable d1. Because the first step doesn't work, it's in fact: take the 'unsigned int' value 2147483648, negate it, convert that to 'signed int', then store the result in the freshly created int variable d1. The 'conversion to signed' has implementation-defined results by the way. For extra fun, see the #define of INT_MIN in /usr/include/machine/limits.h and the explaining comment above it. [...] To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message