Behavior is undefined ... compiler is free to evaluate arguments to a function in any order it pleases - there is no sequence point in between
http://en.wikipedia.org/wi
Main Topics
Browse All TopicsHi,
The output for the following code is 2,3,3. Can anybody explain why?
#include <iostream>
using namespace std;
void CPrint(int a, int b, int c)
{
cout << a;
cout << b;
cout << c;
}
void main()
{
int i=0;
CPrint(i++,++i,++i);
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Behavior is undefined ... compiler is free to evaluate arguments to a function in any order it pleases - there is no sequence point in between
http://en.wikipedia.org/wi
When a postfix operator is applied to a function argument, the value of the argument is not guaranteed to be incremented or decremented before it is passed to the function.
see
http://msdn.microsoft.com/
Hi,
The result you specified is 2,2,3 . But if you compile this in VC++ 6.0 the result is
2,2,1. This result is same for Gcc compiler.
i.e different compiler shows different output because the behaviour is unspecified.
try using
i++ + ++i . this is also unspecified behavior
According to bjarne Stroustrap (Author of C++)
Its undefined !! Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined.
> i++ + ++i . this is also unspecified behavior
No, it is undefined behavior
Unspecified behavior means
behavior, for a well-formed program construct and correct data, that depends on the implementation. The implementation
is not required to document which behavior occurs.
Undefined behavior means
behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International
Standard imposes no requirements.
In your case, the reason you get 2,3,3 is:
All parameters are evaluated before the function is called.
If the parameters are evaluated from left to right you have the sequence of operations:
i++
++i
++i
So clearly you have two ++i's which MUST happen before anything else.so the compiler is allowed to optimise them to:
i += 2;
which is slightly more efficient. This is why the first parameter is a 2.
If the parameters are then pushed onto the stack from left to right so the i++ must complete its evaluation before the second parameter is pushed, thus '3'.
Paul
I disagree. Split should be ozo http:#23146571 , Me http:#23146572 and Paul http:#23151537
http:#23147116 contains potentially misleading information
>Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined.
i = i + i; reads value of i twice and writes it too but expression is well defined. Problem is not reading it twice but writing to it more than once with no sequence point in between.
Business Accounts
Answer for Membership
by: ozoPosted on 2008-12-11 at 00:32:30ID: 23146571
Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.
If a shall or shall not requirement that appears outside of a constraint is violated, the
behavior is undefined.
undefined behavior
behavior, upon use of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirements
Since the behavior is undefined, the implementation is allowed to do anything