Please be easy on me and don't shoot me as I'm still newbie.

I'm totally confused and can't for life figure out why when I run this code:

y = 9;
cout << "++y = " << ++y << "\n--y = " << --y << "\ny++ = " << y++ << "\ny-- = " << y-- << "\n";
cout << "y = " << y << "\n";

I get the following results:

y = 9
++y = 9
--y = 9
y++ = 8
y-- = 9
y = 9

instead of these results:

y = 9
++y = 10
--y = 9
y++ = 9
y-- = 10
y = 9

That I get from this code:

y = 9;
cout << "y = " << y << "\n";
cout << "++y = " << ++y << "\n";
cout << "--y = " << --y << "\n";
cout << "y++ = " << y++ << "\n";
cout << "y-- = " << y-- << "\n";
cout << "y = " << y << "\n";

Can anyone explain -in simple words as possible- what happens in the first code so that it prints the result that way?

Can anyone explain -in simple words as possible- what happens in the first code so that it prints the result that way?

Undefined behavior.

Undefined behavior.

But how could, simple increment & decrement operations invoke an undefined behavior? Just curious to know...

The order in which they are to be evaluated is not defined by the language definition. Therefore, it is undefined.

commented: Thanks..... +3

The order in which they are to be evaluated is not defined by the language definition. Therefore, it is undefined.

Does that mean portability of the program is compromised?

In an enormous way.

In an enormous way.

Solution?

Write good code that doesn't contain undefined behaviour.

Thanks for the answers.
We were asked to give the output of the first code by our teacher in a test, I thought it will output as the second code, and I lost the mark of that question.

What I understood from the answers is that there is no way to know the exact out put of such code, am I correct?

If that is the case, I guess we are in real trouble as I'm pretty sure we are going to get a similar question in the final exam about two weeks later.

Any suggestions/ideas?

The correct answer with such code is to state that it invokes undefined behaviour that cannot be relied on and will probably be different on different compilers.

If that is the case, I guess we are in real trouble as I'm pretty sure we are going to get a similar question in the final exam about two weeks later.

The correct answer is that the first set of code above exhibits undefined behaviour. If your teacher doesn't like that answer, you've got a problem. Maybe you should politely tell them that they don't know what they're talking about.

Write good code that doesn't contain undefined behaviour.

Thanks for letting me know that. I thought i have to make another language, then bootstrap a compiler from it, since i can't do it with c++. :P

What i asked is, how to write the the code, which don't give a undefined behavior still giving the correct output for this exact question.

What i asked is, how to write the the code, which don't give a undefined behavior still giving the correct output for this exact question.

The undefined behavior comes from modifying the same variable more than once in the same expression. To get rid of undefined behavior, first figure out what output you want, then break up the expression to do it. In the first post on this thread, the first code snippet has undefined behavior and the second code snippet is the right way to fix it.

The correct answer with such code is to state that it invokes undefined behaviour that cannot be relied on and will probably be different on different compilers.

The correct answer is that the first set of code above exhibits undefined behaviour. If your teacher doesn't like that answer, you've got a problem. Maybe you should politely tell them that they don't know what they're talking about.

Yeah, I guess I'm going try and talk to him about that, else I really have got a problem. :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.