Path: utzoo!attcan!uunet!husc6!cmcl2!nrl-cmf!ames!lll-tis!daitc!jkrueger From: jkrueger@daitc.ARPA (Jonathan Krueger) Newsgroups: comp.std.c Subject: nonportable code or incorrect compilers? Keywords: portable language standard Message-ID: <133@daitc.ARPA> Date: 24 Jun 88 21:23:44 GMT Organization: Defense Applied Information Technology Center, Alexandria VA Lines: 41 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 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? The relevant K&R references appear to be 2.10, Assignment Operators and Expresssions: "If e1 and e2 are expressions, then e1 op= e2 is equivalent to e1 = (e1) op (e2) except that e1 is computed only once." and 2.7, Type Conversions: "float is converted to double, then if either operand is double, the other is converted to double, and the result is double" Putting it all together, does "e1 is computed only once" imply lack of ordinary automatic type conversion for e1 and expressions of which it is a part? Or does "computed" mean "value derived for", and K&R in no way implies different type handling from the unabbreviated expression? And does ANSI clear up this ambiguity? -- Jon Krueger