We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,449 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pre / Post Increment Explanation

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?

5
Contributors
12
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
14
Views
Question
Answered
XO39
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Undefined behavior.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,408
Skill Endorsements: 55

Undefined behavior.

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

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

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

Moschops
Posting Shark
949 posts since Sep 2008
Reputation Points: 317
Solved Threads: 180
Skill Endorsements: 5

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?

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

In an enormous way.

Moschops
Posting Shark
949 posts since Sep 2008
Reputation Points: 317
Solved Threads: 180
Skill Endorsements: 5

In an enormous way.

Solution?

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

Write good code that doesn't contain undefined behaviour.

Moschops
Posting Shark
949 posts since Sep 2008
Reputation Points: 317
Solved Threads: 180
Skill Endorsements: 5

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?

XO39
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Moschops
Posting Shark
949 posts since Sep 2008
Reputation Points: 317
Solved Threads: 180
Skill Endorsements: 5

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.

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

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.

deceptikon
Challenge Accepted
Administrator
3,502 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58

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

XO39
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Moschops, PrimePackster, Narue and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0987 seconds using 2.81MB