R
Red Dragon
Can anybody tell me
if x = x +1 is x++
What is x = x + 2 ?
Thanks
Khoon.
if x = x +1 is x++
What is x = x + 2 ?
Thanks
Khoon.
Can anybody tell me
if x = x +1 is x++
What is x = x + 2 ?
Red said:Can anybody tell me
if x = x +1 is x++
What is x = x + 2 ?
The effect may be equivalent but in the former x is evaluated twice
whereas in the latter x is evaluated once.
actually, no. it's read once and modified once in both cases.
Jordan said:actually, no. it's read once and modified once in both cases.
Robert said:As I said, x is *evaluated* twice in the first expression, once in the
second.
Robert Gamble
*Evaluated * by the compiler? Or machine code?
Most compilers I know generate the same assembly in both cases, even with
optimisation turned off.
Evaluated by the abstract machine as per the C Standard.
An implementation does not have to evaluate part of an expression if
the result of the expression can be deduced without doing so and if the
program cannot tell the difference, irrespective of optimizations. In
other words, x may be evaluated a _maximum_ of 2 times in the first
expression and once in the second. It is therefore obviously possible
that both operations result in x being evaluated the same number of
times, namely one.
Robert Gamble
Jordan said:The compiler can evaluate it as many or as few times as it pleases, as
long as it acts "as if" it only did it as many times as you said. I take
"evaluate" to mean "get the value of", and assigning to a variable does
not "evaluate" it, it "assigns" it.
I take
"evaluate" to mean "get the value of", and assigning to a variable does
not "evaluate" it, it "assigns" it.
Mark McIntyre said:In order to assign the value to a new variable, it has to evaluate the
previous variable first. Computing isn't magic.
Given
x = y;
y is evaluated, but x is not. (Specifically, the previous value of x
is not evaluted.)
I agree.
Keith Thompson said:Given
x = y;
y is evaluated, but x is not. (Specifically, the previous value of x
is not evaluted.)
...that's what i said. why's he right and i'm not?
Red said:Can anybody tell me
if x = x +1 is x++
What is x = x + 2 ?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.