Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!husc6!mit-eddie!ll-xn!ames!ucbcad!ucbvax!CORY.BERKELEY.EDU!dillon From: dillon@CORY.BERKELEY.EDU (Matt Dillon) Newsgroups: comp.sys.atari.st Subject: Re: Megamax bug or crappy C ? Message-ID: <8705201814.AA22619@cory.Berkeley.EDU> Date: Wed, 20-May-87 14:14:08 EDT Article-I.D.: cory.8705201814.AA22619 Posted: Wed May 20 14:14:08 1987 Date-Received: Sat, 23-May-87 07:22:33 EDT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 49 > array[i - 1] = array[i--] - 1; /* Trouble here. */ >I looked at the code the compiler brewed and it appeared that first it >computes array[i--] - 1, including the post decrement, and then (with i >already decremented !) it computes array[i - 1]. So array[i - 1], really >becomes array[i - 2] ! As far as I know the post decrement should take >place after the entire expression is evaluated (the Alcyon compiler does >that). But I am not absolutely sure whether K & R is unambiguously clear >about this (I thought it said that the decrement takes place after the >assignment is done, but I don't have K & R laying around here, sorry). >Anyone ? Totally and Completely Wrong. You can make no assumptions as to the order of evaluation for most C statements. Except for &&, ||, the comma operator, and a very few other statements/operations, the order of evaluation is totally dependant on the C compiler used. The definition for post increment or decrement is that the particular variable's value is used in that instance and then decremented or incremented by the end of the expression (segment). You can't even assume that the order of evaluation for an assignment is right-side-first: *ptr = *ptr++ Could either evaluate the right side first, or evaluate the effective address of the left side, then the right side, then do the assignment. a && b && c a is evaluated if TRUE b is evaluated, and if that is TRUE c is evaluated. Thus, char *a=NULL; if (a && *a) ... is perfectly valid C.... the *a is not evaluated if a is NULL. a || b || c a is evaluated if FALSE b is evaluated, etc... (a,b) a is evaluated. b is evaluated, the result of the expression is b. So the only time you can use postincrement/postdecrement and *know* the evaluation order would be in something like this (example): if (a++ || b++ || c++) .. for (i++, j = i; j < 10; j++) .. -Matt P.S. somebody care to check if the Alcyon compiler does the right thing for ||,&&, and the comma operator??