# Help

Discussion in 'C Programming' started by Red Dragon, Nov 10, 2005.

1. ### Red DragonGuest

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

Thanks
Khoon.

Red Dragon, Nov 10, 2005

2. ### GiovanniGuest

On 11/10/05 16:49, Red Dragon wrote:
> Can anybody tell me
> if x = x +1 is x++
> What is x = x + 2 ?
>

If applicable is: x += 2;
Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 <http://counter.li.org/>

Giovanni, Nov 10, 2005

3. ### Robert GambleGuest

Red Dragon wrote:
> 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

Robert Gamble, Nov 10, 2005
4. ### Jordan AbelGuest

On 2005-11-10, Robert Gamble <> wrote:
> Red Dragon wrote:
>> 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.

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

Jordan Abel, Nov 10, 2005
5. ### Red DragonGuest

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

>
> actually, no. it's read once and modified once in both cases.

I got it. Thanks for your help.

Regards,
Khoon.

Red Dragon, Nov 10, 2005
6. ### Robert GambleGuest

Jordan Abel wrote:
> On 2005-11-10, Robert Gamble <> wrote:
> > Red Dragon wrote:
> >> 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.

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

Robert Gamble, Nov 11, 2005
7. ### Guest

Robert Gamble wrote:
> Jordan Abel wrote:
> > On 2005-11-10, Robert Gamble <> wrote:
> > > Red Dragon wrote:
> > >> 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.

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

*Evaluated * by the compiler? Or machine code? Most compilers I know
generate the same assembly in both cases, even with optimisation turned
off.

, Nov 11, 2005
8. ### Robert GambleGuest

wrote:
> Robert Gamble wrote:
> > Jordan Abel wrote:
> > > On 2005-11-10, Robert Gamble <> wrote:
> > > > Red Dragon wrote:
> > > >> 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.
> > >
> > > 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

>
> *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

Robert Gamble, Nov 11, 2005
9. ### Jordan AbelGuest

On 2005-11-11, Robert Gamble <> wrote:
> wrote:
>> Robert Gamble wrote:
>> > Jordan Abel wrote:
>> > > On 2005-11-10, Robert Gamble <> wrote:
>> > > > Red Dragon wrote:
>> > > >> 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.
>> > >
>> > > 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

>>
>> *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
>

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.

Jordan Abel, Nov 11, 2005
10. ### Robert GambleGuest

Jordan Abel wrote:
> On 2005-11-11, Robert Gamble <> wrote:
> > wrote:
> >> Robert Gamble wrote:
> >> > Jordan Abel wrote:
> >> > > On 2005-11-10, Robert Gamble <> wrote:
> >> > > > Red Dragon wrote:
> >> > > >> 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.
> >> > >
> >> > > 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
> >>
> >> *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
> >

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

Robert Gamble, Nov 11, 2005
11. ### Mark McIntyreGuest

On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
<> wrote:

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

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Nov 11, 2005
12. ### Keith ThompsonGuest

Mark McIntyre <> writes:
> On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
> <> wrote:
>
>>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.

Given

x = y;

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

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Nov 11, 2005
13. ### Mark McIntyreGuest

On Fri, 11 Nov 2005 19:12:53 GMT, in comp.lang.c , Keith Thompson
<> wrote:

>Mark McIntyre <> writes:
>> On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
>> <> wrote:
>>
>>>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.

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

I agree.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Nov 11, 2005
14. ### Jordan AbelGuest

On 2005-11-11, Mark McIntyre <> wrote:
> On Fri, 11 Nov 2005 19:12:53 GMT, in comp.lang.c , Keith Thompson
> <> wrote:
>
>>Mark McIntyre <> writes:
>>> On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
>>> <> wrote:
>>>
>>>>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.

>>
>>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?

Jordan Abel, Nov 11, 2005
15. ### SM RyanGuest

# >>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
register or whatever.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.

SM Ryan, Nov 11, 2005
16. ### Tim RentschGuest

Keith Thompson <> writes:

> Mark McIntyre <> writes:
> > On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
> > <> wrote:
> >
> >>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.

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

Tim Rentsch, Nov 11, 2005
17. ### Mark McIntyreGuest

On Fri, 11 Nov 2005 23:08:02 +0000 (UTC), in comp.lang.c , Jordan Abel
<> wrote:

>>>> On Fri, 11 Nov 2005 07:34:46 +0000 (UTC), in comp.lang.c , Jordan Abel
>>>> <> wrote:
>>>>
>>>>>I take
>>>>>"evaluate" to mean "get the value of", and assigning to a variable does
>>>>>not "evaluate" it, it "assigns" it.

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

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Nov 12, 2005
18. ### Charles RichmondGuest

Red Dragon wrote:

> 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???