Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: Sizes, alignments, and maxima (was: Contiguous Arrays) Message-ID: <1989Feb26.012243.12894@utzoo.uucp> Organization: U of Toronto Zoology References: <2508@ssc-vax.UUCP> <340009@hplvli.HP.COM> <1839@valhalla.ee.rochester.edu> Date: Sun, 26 Feb 89 01:22:43 GMT In article <1839@valhalla.ee.rochester.edu> badri@valhalla.ee.rochester.edu (Badri Lokanathan) writes: > unsigned i, j, k, val; > > i = 100; j = 200; k = 300; > > val = i - j + k; > printf("%u\n", val); > > val = (i - j) + k; > printf("%u\n", val); > > val = i - j; > printf("%u\n", val); >} > >The output was >200 >200 >4294967196 > >Thus even though the intermediate value was rubbish (-100), it still >worked correctly. (I checked the assembler output for difference too.) >Could anybody tell me of a machine where this will not run? No ANSI-conforming implementation will refuse to run this. The intermediate result was neither rubbish nor -100, it was 4294967196, which is perfectly correct, legal, and standard-compliant. Arithmetic on unsigned numbers is defined to wrap around rather than causing overflow. If you tried the equivalent with signed arithmetic, on some machines (I believe the MIPS machines are among them), you would get overflow traps. I can't immediately think of any currently-extant machines where the same would happen with pointers, but that's no guarantee that there never will be one. -- The Earth is our mother; | Henry Spencer at U of Toronto Zoology our nine months are up. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu