Hello
The output is 10. Why? In Java/PHP the result is 9. For me it`s 9 too. Can i find the reason in the priority rules?Code:#include <iostream.h> int main() { int c=3; cout << (((++c)) + ((++c))) << endl; return 0; }
Thanks
sphreak
Hello
The output is 10. Why? In Java/PHP the result is 9. For me it`s 9 too. Can i find the reason in the priority rules?Code:#include <iostream.h> int main() { int c=3; cout << (((++c)) + ((++c))) << endl; return 0; }
Thanks
sphreak
The ++ is processed before the + operator, and since there are two, this is the order of ops:
++c
++c
c+c
making:
++c---c is now 4
++c---c is now 5
c+c---result 10
The expression is ambiguous.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I just did a search, msdn's got the order of ops documented:
http://msdn.microsoft.com/library/de...evaluation.asp
Bjarne Stroustrup says that for the post-increment case this expression is undefined:
http://www.research.att.com/~bs/bs_f...aluation-order
Considering "if you read a variable twice in an expression where you also write it, the result is undefined", it means that the expression is also undefined for the pre-increment case.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah, I wasn't aware of that. *makes mental note for his compiler*
I suppose that makes sense though, but it seems sites like msdn have just gone and created their own order of operations to define those types of equations. And it appears Stroustrup probably took this ambiguity in to account when he put that up there, judging by the ambiguity witnessed here.
It's nothing to do with order of operation (or how many parentheses you use for that matter).
http://www.eskimo.com/~scs/C-faq/s3.html
If you attempt to modify the same object more than once between sequence points, the result is undefined - PERIOD.