# expression

here is the following statement

i = 5;
j = i++ - ++i;

What are the possible values of j in C++?

My guess would be 0 because if I increase i then subtract i then i should get 0, am I right?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>>What are the possible values of j in C++?

In C++ that statement produces undefined behavior (IAW C++ standard).
So the value can be anything.
0
Commented:
The answer is ambiguous, but saying it is implementation defined what happens is a far cry from saying it can be anything.
No compiler writer is going to put a random number in there.

The question is, when is i++ executed.
The answer is, sometime after i is used and the semicolon. That means there are two possible answers for the expression.

Your first step should be to compile a little program and see for yourself. And you're right, zero is one possible answer.

Suppose it was rewritten:

j=i - ++i;
i++;

Suppose it was written:
int temp = i++;
j = temp - ++i;

What then?
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Yes you are right...

see how it is processed....

j = i++ - ++i;
will be equivalent to
j = i++ - (Increaced Value of i;
and now i is 6

j = i++ - 6;

now it will be equivalent to

j= (Value of i - 6; and then Increase i

so j will be 0 and i will be 7

0
Commented:
dbkruger,

>Suppose it was rewritten:

>j=i - ++i;
>i++;

>Suppose it was written:
>int temp = i++;
>j = temp - ++i;

>What then?

int temp = i++; this will assign the old value 5 to temp and then increase i to 6;

j = temp - ++i; will be equivalent to

j = 5 - ++i; and this will be equibalent to

j = 5 - (Increace value of i);

and finally

j= 5 - 7 = -2

0
Commented:
>>The answer is ambiguous, but saying it is implementation defined what happens is a far cry from saying it can be anything.

Yes, it is a bit of a strech to truely believe that it can equal to anything.
However, the point I'm trying to make, is that you should not care what it equals to, because you should avoid using implementation defined code, if it's not absolutely required.
Clearly this type of code usage is not absolutely required, and you can change the code to give you well defined behavior, instead of implementation defined behavior.

However, technically speaking, implementation defined means the implementation can do anything it wants, and therefore the results can be anything the implementation desires it to be.
I'm not saying any compiler will do this, but I am saying that a compiler can do it, and still be considered compliant to the C++ standard.
0
Commented:
So there you have it.
0
Commented:
FYI:
A good example of why you should not use implementation defined behavior, is the modification of a string literal.
Example:
char* data = "Hello World";
strcpy(data, "Good by");

The above code results in implementation defined behavior.
Many years ago, before VC++ 6.0 came out, many season C++ programmers continued to warn developers about using this type of code.
However, since VC++ 5.0 happly ran above code with no runtime errors, many developers continued to use such implementation defined code.
When VC++ 6.0 came out, it did not produce the same behavior, and infact it causes a runtime error with above code.
So developers who try to compile the same code in VC++ 6.0, whould get a runtime error, when they previously did not.

So when you're dealing with implementation defined code, you not only have to worry about how the code will work from one compiler to the next, but you also have to worry about how the code will work from one version of the compiler to the next.
Even though VC++ 5.0 and VC++ 6.0 have different results with the above code, both are considered compliant with the standard with their results.

When developers first started complaining about this issue, one of the first questions they asked, was which compiler is right (5.0 or 6.0)?
And the answer is both are right, because they can do anything they want with undefined behavior.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.