Subject: Re: off_t and int/long
To: None <hiroy@netcom.com, port-i386@NetBSD.ORG>
From: Arne H. Juul <arnej@pvv.unit.no>
List: port-i386
Date: 05/19/1996 00:10:01
(Example program with "strange" output:)
> % cat xxx.c
> /* */
> #include <stdio.h>
>
> long b = 1 << 24;
> u_long c = 1 << 9;
> int d;
>
> void
> f(off_t a)
> {
> printf("a=0x%016qx\n", a);
> }
>
> int
> main()
> {
> for(d = 1; d >= -1; d -= 2) {
> f(b * c + d);
> f((off_t)(b * c + d));
> f((off_t)b * c + d);
> f(b * (off_t)c + d);
> f(b * c + (off_t)d);
> }
> return (0);
> }
> % cc xxx.c
> % ./a.out
> a=0x0000000000000001
> a=0x0000000000000001
> a=0x0000000200000001
> a=0x0000000200000001
> a=0x0000000000000001
> a=0x00000000ffffffff
> a=0x00000000ffffffff
> a=0x00000001ffffffff
> a=0x00000001ffffffff
> a=0xffffffffffffffff
Yes. Welcome to the wonderful world of Ansi C integral promotions.
This is entirely correct and the expected result.
Not exactly *intuitive*, mind you, but nevertheless right. Any Ansi
C compiler should give the same results, and I just tried Sun, SGI,
and DEC with the same results.
- Arne H. J.