nonportable code or incorrect compilers?

David Haynes david at geac.UUCP
Fri Jul 8 22:06:26 AEST 1988


In article <430 at uwovax.uwo.ca> 16012_3045 at uwovax.uwo.ca (Paul Gomme) writes:
>In article <133 at daitc.ARPA>, jkrueger at 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-



More information about the Comp.std.c mailing list