|
-
Mar 27th, 2003, 11:00 PM
#1
Thread Starter
Member
confuse
#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
Last edited by Little; Mar 28th, 2003 at 08:14 AM.
-
Mar 28th, 2003, 07:57 AM
#2
Frenzied Member
Re: confuse
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;
}
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 28th, 2003, 08:03 AM
#3
Thread Starter
Member
Sorry...i mean the output is not 16...
i use vc++ to compile it...
the output is 17....
that's y i dun understand...
Last edited by Little; Mar 28th, 2003 at 08:09 AM.
-
Mar 28th, 2003, 10:10 AM
#4
Frenzied Member
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.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 28th, 2003, 10:45 AM
#5
Thread Starter
Member
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
-
Mar 28th, 2003, 11:28 AM
#6
Frenzied Member
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;
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Mar 28th, 2003, 12:25 PM
#7
Thread Starter
Member
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...
-
Mar 28th, 2003, 12:47 PM
#8
Frenzied Member
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.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Mar 28th, 2003, 12:55 PM
#9
Thread Starter
Member
yaya...it makes no sense....
i am quite confuse about this....
how the code being execute....sigh....
-
Mar 29th, 2003, 05:10 AM
#10
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 29th, 2003, 07:49 AM
#11
Monday Morning Lunatic
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...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|