|
-
March 31st, 2006, 04:54 PM
#1
++x + ++x
Hi, This is my first post, and hope not to be the last.
Any one can tell me the result of this code, and the most importent: Why??
the code is:
=======================
int x = 3;
cout << ++x + ++x <<endl;
cout << x;
=======================
please test it before replying
-
March 31st, 2006, 05:14 PM
#2
Re: ++x + ++x
Is this a homework assignment?
-
March 31st, 2006, 05:19 PM
#3
Re: ++x + ++x
 Originally Posted by YourSurrogateGod
Is this a homework assignment?
No its NOT!!
I was studing for the next week exam, and this problem showed up.
if you know any thing about it.. i'll be glad to hear it!!
-
March 31st, 2006, 05:19 PM
#4
Re: ++x + ++x
The code does not compile. Okay, no kidding
Undefined behaviour... why? Because the C++ standard says so.
 Originally Posted by C++ Standards Section 5 - Expressions para(4)
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the sub-expressions of a full-expression; otherwise the behavior is undefined.
[Example:
Code:
i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // ‘i’ becomes 9
i = ++i + 1; // the behavior is undefined
i = i + 1; // the value of ’i’ is incremented
—end example]
Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 31st, 2006, 05:24 PM
#5
Re: ++x + ++x
just check out in MSDN for Operator Precedence and Associativity and see how it will work.
Code:
int main()
{
int x=1;
cout << ++x + ++x <<endl; //produce output 6
return 0;
//to run this program i used VC6.0 and it's perfectly valid
}
Thankyou
Last edited by humptydumpty; March 31st, 2006 at 05:27 PM.
-
March 31st, 2006, 05:28 PM
#6
Re: ++x + ++x
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 31st, 2006, 05:32 PM
#7
Re: ++x + ++x
Exterminator:
why doesn't it compile? undefined behavior!!
it did compile with me, but the result weren't as expected
humptydumpty:
I checked there and i know that it has some thing to do with the operators precedency... but there are nothing about such case.
All:
I'll be more specific:
preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
So, when we say :
++x + ++x
this should be compiled and interpruted as:
5 + 4
then the result is:
9
but when you test it with C++ compiler : the result is 10
this is the problem... and this is what i know.
If i'm mistaken,, please correct me!
-
March 31st, 2006, 05:38 PM
#8
Re: ++x + ++x
 Originally Posted by Omarkth
it did compile with me, but the result weren't as expected
What compiler are you using and what were the unexpected results?
-
March 31st, 2006, 05:41 PM
#9
Re: ++x + ++x
 Originally Posted by Omarkth
preincrement operator (++) has precedenc over the addition operator (+) but it compile from right to left,
So, when we say :
++x + ++x
this should be compiled and interpruted as:
5 + 4
then the result is:
9
but when you test it with C++ compiler : the result is 10
this is the problem... and this is what i know.
No. If I remember correctly, pre/post increment operators involved in function calls are undefined as to when they will be executed. For example:
Code:
int h = 2;
f = g(++h) + i(++h);
If 'h' is in int, and 'g' and 'i' are functions, what value is passed to 'g' and what value is passed to 'i'?
I've run into bugs in code, for this exact problem. The code "worked" on one platform, but didn't "work" on another, because of the compiler's implementation of function calls and the pre-increment operator.
Viggy
-
March 31st, 2006, 05:46 PM
#10
Re: ++x + ++x
I'm using VC6.0....
Okey, MrViggy: can you explain why the result came out like that??
if you wrote:
x = 3;
cout << ++x << ++x;
OUTPUT: 54
but if you make it:
x=3;
cout << ++x + ++x;
OUTPUT: 10 // insted of 5+4=9 ???? WHY??
-
March 31st, 2006, 05:55 PM
#11
Re: ++x + ++x
5 + 5 == 10
In other words, the compiler did the pre-increments first, then the addition.
Viggy
-
March 31st, 2006, 05:58 PM
#12
Re: ++x + ++x
that's what i'm asking about :
why 5 + 5???
why not 5 + 4 ??
x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5
u see what i'm talking about?
-
March 31st, 2006, 06:14 PM
#13
Re: ++x + ++x
 Originally Posted by Omarkth
that's what i'm asking about :
why 5 + 5???
why not 5 + 4 ??
x originally was 3 : first increment made it 4, second made it 5 ===> must be 4 + 5 , not 5 + 5
u see what i'm talking about?
Please re-read my post again. I was kidding when I said your code does not compile. In fact, it would not compile in the state you have provided it to us. But considering the fact that there is program that has such statement - the answer is what followed in the later lines (next to first one - the kidding one ).
It is an undefined behaviour. Before posting any more please atleast read the quote that i provided and if still un-certain read the threads, the link to which I provided in my next post. That statement is free to give any result on any compiler. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 31st, 2006, 06:17 PM
#14
Re: ++x + ++x
Because the order of those operations is undefined (as was stated earlier), and compiler dependent! Take this example:
Code:
#include <iostream>
int SomeFuncA (int a)
{
std::cout << "In SomeFuncA...\n";
return a;
}
int SomeFuncB (int b)
{
std::cout << "In SomeFuncB...\n";
return b;
}
int main(/*int argc, char *argv[]*/)
{
int tmpVal = 3;
std::cout << "Starting...\n";
std::cout << SomeFuncA(++tmpVal) << SomeFuncB(++tmpVal) << std::endl;
std::cout << "Now, just test the pre-inc...\n";
tmpVal = 3;
std::cout << ++tmpVal << " " << ++tmpVal << std::endl;
return 0;
}
When I run this using Visual Studio 6, I get:
Starting...
In SomeFuncB...
In SomeFuncA...
54
Now, just test the pre-inc...
5 4
The same exact program on Visual Studio 7 outputs:
Starting...
In SomeFuncB...
In SomeFuncA...
55
Now, just test the pre-inc...
5 5
Two different compilers, two different results.
Viggy
-
March 31st, 2006, 06:21 PM
#15
Re: ++x + ++x
thank you MrViggy, this is what i was really waiting for..
thanx again
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
|