Path: utzoo!yunexus!geac!david From: david@geac.UUCP (David Haynes) Newsgroups: comp.std.c Subject: Re: nonportable code or incorrect compilers? Message-ID: <2973@geac.UUCP> Date: 8 Jul 88 12:06:26 GMT Article-I.D.: geac.2973 References: <133@daitc.ARPA> <430@uwovax.uwo.ca> Reply-To: david@geac.UUCP (David Haynes) Organization: /usr/lib/news/organisation Lines: 57 In article <430@uwovax.uwo.ca> 16012_3045@uwovax.uwo.ca (Paul Gomme) writes: >In article <133@daitc.ARPA>, jkrueger@daitc.ARPA (Jonathan Krueger) writes: >> Here's a question of portability and language standards that came up in >> a real life program. According to K&R or ANSI, predict the output of >> the following code: >> >> #define DEFCONST 1.05 >> main() >> { >> int count = 800; >> >> printf("count is %d, ", count); >> count *= DEFCONST; >> printf("now %d\n", count); >> } >> >> My prediction, based on K&R, was the output: >> count is 800, now 839 >> Indeed, the Pyramid 98x produces exactly this output. >> But your mileage may vary. 4.2BSD VAX, Gould, and Sun-3 produce output: ^^^^^^^^^^ >> count is 800, now 800 I don't get this at all! I am running on a VAX 8650 under Ultrix 2.0. My results are: cc non-optimized count is 800, now 839 cc optimized count is 800, now 839 gcc non-optimized count is 800, now 839 gcc optimized count is 800, now 839 vcc non-optimized count is 800, now 839 vcc optimized count is 800, now 839 HOWEVER, my sun 3/50 under Sun O/S 3.2 gives the following: cc non-optimized count is 800, now 800 cc optimized count is 800, now 800 As mentioned in the original article. yechhh! >> Which brings me to the question: is this code non-portable, or do >> three out of four compilers surveyed fail to correctly implement K&R? > > Just to stir things up a little, under VAX/VMS, I get an answer >of 839; Turbo C gives an answer of 840! Now, if on the sun, I s/count *= DEFCONST/count = count * DEFCONST/ I get the following: cc non-optimized count is 800, now 840 cc optimized count is 800, now 840 This is even more interesting! -david-