what is the output

What is the output 6 or 5 and why ?
1
2
3
4
int number = 6;
int x = 0;
x = number--;
cout << x << endl;
so the answer is 6 yes ? I mean the operators will only take affect if it was placed before number. take this for example :

1
2
int number = 6;
cout << number++ << endl;


answer would still be 6
?
I wouldn't write code like that. IIRC it is up the the compiler whether it will do the x operator= or number-- first.


Write this instead:
number = x = number - 1

Both x and number = 5


EDIT:

It seems that if you write -- or ++ before the variable it takes priority. I still wouldn't write code like that.
Last edited on
I dont have visual studio atm. But are you suggesting 5 as an answer ? Why ?
Prefix vs suffis btw have you tried compiling it?
Topic archived. No new replies allowed.