dsill@NSWC-OAS.arpa (Dave Sill) (02/26/88)
In article <4140@ptsfa.UUCP> Dave Turner <ptsfa!dmt> writes: >I've always found it to be useful to look at ++x and x++ and say: > > ++x increment x before using it in another expression in the > same statement > >and > > x++ use the present value of x in any other expressions in this > statement before incrementing x. > X in the next statement will have the new value. > >There's more to be said but the above has been satisfactory for over 11 years. I guess you've been lucky, then. For example: i = 0; for (i++; i <= 10; i++) { printf ("%d ", i); } will print: 1 2 3 4 5 6 7 8 9 10 Clearly, the incremented values of x are being used in the other expressions in the for statement. This is because the side effects of the increment operator are guaranteed to have taken place before the next expression in the same statement or the next statement is executed. In dpANS terminology, the end of an expression is a sequence point, and at a sequence point all side effects must have taken place. I'd suggest the following: ++x increment x before using it. x++ increment x after using it, but before the next expression Note: the results are undefined if x is used elsewhere in the same expression. ========= The opinions expressed above are mine. "We must remove the TV-induced stupor that lies like a fog across the land." -- Ted "Hypertext" Nelson