Help

R

Robert Gamble

Red said:
Can anybody tell me
if x = x +1 is x++

The effect may be equivalent but in the former x is evaluated twice
whereas in the latter x is evaluated once. Additionally, the result of
evaluating the former is the same as ++x, not x++.
What is x = x + 2 ?

x += 2.

Robert Gamble
 
J

Jordan Abel

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.
 
R

Robert Gamble

Jordan said:
actually, no. it's read once and modified once in both cases.

As I said, x is *evaluated* twice in the first expression, once in the
second.

Robert Gamble
 
S

slebetman

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.
 
R

Robert Gamble

*Evaluated * by the compiler? Or machine code?

Evaluated by the abstract machine as per the C Standard.
Most compilers I know generate the same assembly in both cases, even with
optimisation turned off.

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
 
J

Jordan Abel

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

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.
 
R

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.

Well, you are wrong on both counts and it doesn't matter what you
consider evaluate to mean. If you care to actually read what the
Standard has to say you should start by reading the following
paragraphs:

5.1.2.3p3
6.5.2.4p2
6.5.16.2p3

Robert Gamble.
 
M

Mark McIntyre

I take
"evaluate" to mean "get the value of", and assigning to a variable does
not "evaluate" it, it "assigns" it.

In order to assign the value to a new variable, it has to evaluate the
previous variable first. Computing isn't magic.
 
K

Keith Thompson

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.)
 
S

SM Ryan

# >>Given
# >>
# >> x = y;
# >>
# >>y is evaluated, but x is not. (Specifically, the previous value of x
# >>is not evaluted.)
# >
# > I agree.
#
# ...that's what i said. why's he right and i'm not?

The lvalue x and rvalue y are evaluated.

For example if an lvalue is a[i++], i will be incremented. Evaluation of
an lvalue does not include loading whatever is at the address into a
register or whatever.
 
T

Tim Rentsch

Keith Thompson said:
Given

x = y;

y is evaluated, but x is not. (Specifically, the previous value of x
is not evaluted.)

In the technical language of the Standard, I think it's more
accurate to say that both x and y are evaluated, but only the
evaluation of y results in a conversion of the lvalue to the
value stored (which results in a read access of y's object).

(In case someone wants a reference - try 6.3.2.1 p1,p2.)
 
M

Mark McIntyre

...that's what i said. why's he right and i'm not?

Because it wasn't how what you wrote, read.

Your words, quoted above, implied to me that assigning variable X to
variable Y didn't evaluate X, it only assigned it. I believe we can
agree that this is wrong.
 
C

Charles Richmond

Red said:
Can anybody tell me
if x = x +1 is x++
What is x = x + 2 ?

x++; x++;


Question: If the king sits on gold, who sits on silver???



Answer: The Lone Ranger
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top