Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!ames!sdcsvax!ucbvax!hplabs!sdcrdcf!ism780c!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Possible C Anomaly Message-ID: <1313@haddock.ISC.COM> Date: Mon, 5-Oct-87 12:39:34 EDT Article-I.D.: haddock.1313 Posted: Mon Oct 5 12:39:34 1987 Date-Received: Fri, 9-Oct-87 00:42:10 EDT References: <9601@brl-adm.ARPA> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 31 In article <9601@brl-adm.ARPA> mrd@clutx.clarkson.EDU (Michael R. DeCorte) writes: >I might simply missing something but I still believe [ k = --k ] is >correct. It does not matter which is evaluted first. It is incorrect. The value of the expression is (oldk - 1), with TWO side effects (decrement k, and store (oldk - 1) in k). It would be legal for the compiler to perform the store and then the decrement, leaving (oldk - 2) in k. >The way I would actully code it is rather non C'ish >if (k > 1) k--; else k = 1; By "non C'ish" you seem to mean that it isn't obscure enough? I think it's both C'ish and the best way to write it if you want to preserve those exact semantics. But I'd just write "if (k > 1) --k;", if it's known that k > 0 (as was implied, I think, by the original problem statement). >And to give yet another solution you could also try [...] or > (k-- <= 1) ? k = 1 : ; >Although not very readable this has the advantage that it will very >good with many cpu's and compilers but very bad with others. As has been pointed out already, this one is illegal; you have to either use a dummy expression (": 0;") or use the if-else syntax (which is better anyway, if you aren't in an expression context). As for being "very good with many CPUs/compilers", I think you'd do better with "if (--k <= 0) k=1;" which has the advantages of using a predecrement (so no need to save the old value) and a compare against zero (which is often a side-effect of decrementing anyway). Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint