3 ms·
Not really. ++x (as opposed to x++) is expected to return the incremented value, not the pre-increment value. So your choices are (+ x (incf x)) and (do
by mst 17y ago
Not really. ++x (as opposed to x++) is expected to return the incremented value, not the pre-increment value. So your choices are
(+ x (incf x))
and
(do
(incf x)
(+ x x))
depending on whether the language evaluates arguments in order, or if it builds the arguments as cells to observe and then reads their values (there's presumably a technical term for that which I can't remember - I blame sunday :)
So in C it's undefined behaviour because the compiler should be permitted to handle it whichever ways it wants, but in perl you'll get 21 as expected.
C is great fun for this - people often expect things like function arguments to be evaluated in the order as written but since it's a low level languages the compiler is allowed to do something completely different if it wants (note this something is still "equivalent" in terms of the C spec, just not in terms of the developer's expectations).
This is one of the many reasons -O3 can make apparently correct code start crashing all over the place.
- mahmud 17y agoWhat do you mean by "not really"? :-P The poster's question is a classic comp.lang.c "favorite"; misunderstanding operator precedence and the two flavors of incrementing in C. By the way, the term you're looking for is "binding", and bad languages have a habit of confusing binding and assignment. LET "binds" its arguments to their corresponding values within its body, in a way identical to function application. Algebraically his question looks like this: f(x) -> x + succ(x) y = f(10) Where succ is the incrementing, or successor function, and + is the addition function. Btw, much fun can be had with just the increment and decrement functions, along with a predicate to test for zero equality. http://en.wikipedia.org/wiki/Primitive_recursive_function http://en.wikipedia.org/wiki/Primitive_recursive_function P.S. The Lisp "DO" form is for iteration; PROGN and BEGIN are the main block-structure forms used in Common Lisp and Scheme respectively. To the original poster; invest in learning Standard ML and a few lisp dialects, on your own, they will make your compiler hacking far more enjoyable, and you probably wont waste as much time debugging a bad language design from 1970s that thought formal language research from the 1670s was too cutting edge; New Jersey heard "Leibniz" and they thought "Lebanese".
- zck 17y ago>P.S. The Lisp "DO" form is for iteration; PROGN and BEGIN are the main block-structure forms used in Common Lisp and Scheme respectively. I think mst's using Arc's do, which is progn.
- huhtenberg 17y ago> The poster's question is a classic comp.lang.c "favorite"; misunderstanding operator precedence and the two flavors of incrementing in C. It is a classic C question not knowing about the sequence points and side-effects. It has nothing to do with either operator precedence or lexer behavior.