Question:
Another C++ increment question =)?
2008-02-24 23:10:41 UTC
x =1;
x += ++x + ++x - x--;

what would x now be? my compiler is telling me 5 but using Bradleys answer I am doing:
increment x twice so x = 3.
x += 3 + 3 - 3--
x += 3 + 3 - 2
x += 4
x = 3 + 4 = 7
AHHH!
Four answers:
2008-02-24 23:22:47 UTC
x += ++x + ++x - x--



the pre-increment operators get applied first

3 += 3 + 3 - 3

now the post-decrement

6--

which results in

5



Keep in mind that x is just one variable, and, as such, has only one location in memory. Therefore, as there is only one instance of x, it can never have two different values within the same expression.

So it will never be:

1 += 2 + 3 - 1
Bradley
2008-02-24 23:55:56 UTC
Well, I guess I should answer again.



I'm almost afraid to give you an answer on this one because I think the result of your expression could vary by compiler. What I would expect to happen (I think this is the standard but I'm not sure).... Because both ++ have the same precedence they will be evaluated from left to right



When the first ++x is evaluated x is assigned the value of 2 but because the first statement is finished the result is used in the final expression.



or



x += ++1 + ++x - x--



x += 2 + ++(2) - x--

because 2 is the result of ++x the first time and x has been re-defined as 2



x += 2 + 3 - (3)--

Again the result of ++x is 3 it is placed in the expression and assigned to x



x += 2 + 3 - 2

x-- then modifies the value of x to be 2



x = 2 + 3 - 2 + 2



x = 5



By the way I think this would be a real dick move by you professor to put a question like this on a midterm. If you can understand the other questions you posted you should be fine.
Shadow Wolf
2008-02-25 00:32:33 UTC
Ok, I don't understand why you need to know, but precedence can get confusing doing this sort of stuff. The compiler used to test it was DJGPP which is gcc ported to DOS. It runs nice in a command window.



First break it into pieces. Assume each step X is initialized to 1 (x=1)



x+= ++x; x=4

Translating this you get

x=((++x )+ x) or ((2) + 2) = 4

So it increments X then adds the incremented X to get 4.



We add one more step

x+= ++x + ++x; x= 9

x=(((++x) + x) + (++x)) or (((2) + 2) + (5)) = 9

Again it increments X but it doesn't affect the earlier values.



Now we add the final step and it becomes clear.

x+= ++x + ++x - x--; x=5

x=((((++x) + x) + (++x)) - (x--)) or ((((2) + 2) + (5)) - (4)) = 5

You have to keep track of the value of X at each step.



The program used was:

#include

int main(){

int x,y,z;

x=1;

x+= ++x + ++x - x--;

y=1;

y+= ++y;

z=1;

z+= ++z + ++z;

printf("%i %i %i\n",y,z,x);

return 0;

}



Usually the fastest way to understand something in programming is to write a little example program. Breaking things into smaller parts is often the only way you can sort out problems with precedence.



Shadow Wolf
Jain
2008-02-24 23:37:24 UTC
Thats wrong ...

Answer will be

x+=2 + 3 - 3 =2

initially x is 1 so x=x+2 which means 1+2 =3

so the result is 3

i dont know y ur compiler tells wrong...



Did u know postfix and prefix ++x is prefix and x-- is postfix in ur case study abut increment operation then u will understand


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...