#include <iostream>
using std::cout;
int main()
{
int a;
int b = 7;
a = ++b + ++b - --b + --b;
cout << a;
return 0 ;
}
y the output in c++ is 17??
should be 16 right?? anyone may clarify me about this???
thanks in advance ...hehe
Printable View
#include <iostream>
using std::cout;
int main()
{
int a;
int b = 7;
a = ++b + ++b - --b + --b;
cout << a;
return 0 ;
}
y the output in c++ is 17??
should be 16 right?? anyone may clarify me about this???
thanks in advance ...hehe
Please always include your code between code tags, makes it easier to read...
here's your code broken down so you can see why it's 16:
Code:int main()
{
int a;
int b = 7;
a = ++b; //a = 8, b = 8
a += ++b; //a = 17, b = 9
a -= --b; //a = 9, b = 8
a += --b; //a = 16, b = 7
cout << a;
return 0;
}
Sorry...i mean the output is not 16...
i use vc++ to compile it...
the output is 17....
that's y i dun understand...
The output was 16 for me?
Try to use the code below, debug it and step trought the code with f10, and try to watch your window and see what happens.
yaya......
if i use the code u give me...
it absolutely will give 16....
but if u put them in one line ....the output will be 17........
i understand ur code very well...
i just dun know y if i put them all in one line, the output will not be 16...
if follow the precedence and associative rules.....it should be 16 (if put in one line)...
i just dun know y???
u try to use the code that i post there...
izzit there is short circuit code....it just execute the first two prefix code.....and the compiler find the last two prefix code is equal to 0...so it won't execute it....and the output is 17 instead of 16???
vb novice
Well I am getting 14 on VC7 which is what it should be and here is why, it does the all the ++b before the add and sub so you code looks like this:
a = ++b + ++b - --b + --b;
a = 8 + ++b - --b + --b;
a = 9 + 9 - --b + --b;
a = 8 + 8 - 8 + --b;
a = 7 + 7 - 7 + 7;
a = 14 - 7 + 7;
a = 7 + 7;
a = 14;
But y visual c++6....the output is 17????
i try many calculation alredi...it is not possible to get that value..
i just wonder... hehe...
How ODD!!
Well here is what I can figure:
when I step through b = 7 at the end of it so
a = ++b + ++b - --b + --b;
a = ++b + ++b - --b + 7;
The first ++b must be 8
a = 8 + ++b - --b + 7;
if I take a guess and say the remaining --b would be 8 so the second --b = 7
a = 8 + ++b - 8 + 7;
That would make the last ++b 10 then. Which makes no sense. So you got me, I am going to sick with VC7.
yaya...it makes no sense....
i am quite confuse about this....
how the code being execute....sigh....
:rolleyes: ;)
Never use more than one increment/decrement operator on the same variable on one line. It always causes confusion, caused some pointer code of mine not to work for example.
The order they are resolved in is *not* guaranteed, on any compiler. I believe that all the standard requires is that they get evaluated "some time".
Jim can most likely expand on this better than I can...