455,423 Members | 1,697 Online
Need help? Post your question and get tips & solutions from a community of 455,423 IT Pros & Developers. It's quick & easy.

# Help

 P: n/a Can anybody tell me if x = x +1 is x++ What is x = x + 2 ? Thanks Khoon. Nov 15 '05 #1
17 Replies

 P: n/a 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 Nov 15 '05 #2

 P: n/a 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 Nov 15 '05 #3

 P: n/a 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. Nov 15 '05 #4

 P: n/a >>> 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. Nov 15 '05 #5

 P: n/a 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 Nov 15 '05 #6

 P: n/a 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 15 '05 #7

 P: n/a sl*******@yahoo.com 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 Nov 15 '05 #8

 P: n/a On 2005-11-11, Robert Gamble wrote: sl*******@yahoo.com 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. Nov 15 '05 #9

 P: n/a Jordan Abel wrote: On 2005-11-11, Robert Gamble wrote: sl*******@yahoo.com 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. Nov 15 '05 #10

 P: n/a 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 doesnot "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 CLC readme: ----== 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 =---- Nov 15 '05 #11

 P: n/a 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 doesnot "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) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 15 '05 #12

 P: n/a 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 doesnot "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 xis not evaluted.) I agree. -- Mark McIntyre CLC FAQ CLC readme: ----== 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 =---- Nov 15 '05 #13

 P: n/a 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 doesnot "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 xis not evaluted.) I agree. ....that's what i said. why's he right and i'm not? Nov 15 '05 #14

 P: n/a # >>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. -- SM Ryan http://www.rawbw.com/~wyrmwif/ God's a skeeball fanatic. Nov 15 '05 #15

 P: n/a 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 doesnot "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.) Nov 15 '05 #16

 P: n/a 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 CLC readme: ----== 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 =---- Nov 15 '05 #17

 P: n/a 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??? Answer: The Lone Ranger Nov 15 '05 #18

### This discussion thread is closed

Replies have been disabled for this discussion.