Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!husc6!cmcl2!rutgers!labrea!jade!eris!mwm From: mwm@eris.BERKELEY.EDU (Mike (My watch has windows) Meyer) Newsgroups: comp.lang.c Subject: Re: Possible C Anomaly Message-ID: <5112@jade.BERKELEY.EDU> Date: Wed, 16-Sep-87 04:03:08 EDT Article-I.D.: jade.5112 Posted: Wed Sep 16 04:03:08 1987 Date-Received: Fri, 18-Sep-87 06:21:34 EDT References: <9270@brl-adm.ARPA> Sender: usenet@jade.BERKELEY.EDU Reply-To: mwm@eris.BERKELEY.EDU (Mike (My watch has windows) Meyer) Organization: Missionaria Phonibalonica Lines: 47 In article <9270@brl-adm.ARPA> mrd@sun.mcs.clarkson.EDU (Michael R. DeCorte) writes: < /* Example #1 */ k = (k > 1) ? k-- : 1; < < My intent was to decrement k with each pass until it reached 1 and then to < keep it at one. There are a variety of ways to accomplish this but I just < happened to try this one first. 1) ? --k : 1; No! Consider that you're doing (one way) "k = --k". You still don't know whether k will be decremented or assigned to first. Even worse, this statement doesn't make a lot of sense. Try doing another "equivalent" replacement, and get k = (k = k - 1) Why would you want to write something like that? 1) ? (k - 1) : 1; Much better - it's right. I've noticed that may C programmers overuse the "++" and "--" operators. They're nice for changing the value of a variable by one when you want to use the current/resulting value in another expression. If you just want the decremented value, write "k - 1". You can avoid stepping on your own toes that way. Likewise, if you really don't want the value for later use, you might consider "k -= 1" instead of "k--" or "--k". Since this comes up at regular intervals, I think I'll repeat it here: C *does not* guarantee the order of evaluation of an expressions, except for the logical operators. Any expression that changes the value of a variable more than once should be looked at with extreme caution. If logicals don't force order of evaluation, then the resulting code is *not* portable.