Date: Mon, 13 Nov 2000 09:31:19 +0100 From: bdluevel@heitec.net To: cjclark@alum.mit.edu, Salvo Bartolotta <bartequi@inwind.it>, freebsd-questions@FreeBSD.ORG Subject: Re: OT: curious (??) integer types behavio(u)r... Message-ID: <3A0FA6D7.38C88CF7@heitec.net> References: <20001112.21033200@bartequi.ottodomain.org> <20001112143032.Q75251@149.211.6.64.reflexcom.com>
next in thread | previous in thread | raw e-mail | index | archive | help
Crist J . Clark wrote:
>
> On Sun, Nov 12, 2000 at 09:03:32PM +0000, Salvo Bartolotta wrote:
[...]
> > <blockquote>
> > /* A simple test program */
> >
> > #include <stdio.h>
> >
> > 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);
> > }
> >
> > </blockquote>
> >
> > >====> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3A0FA6D7.38C88CF7>
