Question:
C++ Increment and decrement?
2012-09-08 07:49:49 UTC
can someone explain how does c++ get the answer to this equation?

y= x-- + ++x + --x

x = 2

the answer is y = 6
Five answers:
oops
2012-09-08 08:03:26 UTC
Modifying a value more than once, without an intervening sequence point, is undefined behavior.



From the standard, §5/4



    1) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.



You are modifying x three times, with no intervening sequence points. So your code exhibits undefined behavior.



So, no, the answer is not 6. On your compiler, you may get 6, but as far as the C++ standard is concerned, anything whatsoever could happen. I don't just mean you could get any value. I mean ANYTHING could happen. Though, the most disastrous consequence, most likely, is an unpredictable value.



For example, take a look here:

http://ideone.com/A9l4I



As you can see, with the compiler that is used on that website (gcc-4.3.4), the result is not 6, it is 8.



Sequence points:

http://en.wikipedia.org/wiki/Sequence_point





Edit:

Oh, I see roger beat me to it. Why did he get a downvote? People who don't know what they are talking about shouldn't vote(nor answer questions, for that matter). +1 from me.
husoski
2012-09-08 09:47:36 UTC
There is no "the answer". That is not well-defined C++. The existing C++ 2011 standard is based on the 1999 C standard, which still says it's illegal. Few, if any, compilers actually report that as an error, though, and the "error" status has been removed from C as of the 2011 standard, and will presumably be removed from C++ on the next round.



Either way, the result is not defined. You'll get different results from Visual C++ and GNU g++, typically. You may get different results from "debug" versus "optimized" builds on the same compiler. None of those results is better than any of the others.



The general ideas are that (with a few exceptions) C++ does not define the order in which it evaluates the arguments to operators, and also does not specify how long afterward the increment happens in a postfix ++ or -- operation.



In general, don't "use the value of a variable" in one part of an expression and change the value in another part of the same expression; and don't modify a variable more than once in the same expression. This is not exact. It's ok to use a guaranteed previous value of a variable as part of calculation of new value for that variable. x = x + 1; is obviously legal.



The ISO C and C++ standards are prohibitively €xpen$ive, but you can get a look at the free draft of the next verision of C++ at:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf



Don't be put off by the title page. This is so early in the process that the only changes to the C++11 published standard are actually document corrections.



A late draft for the C11 specification can still be found at:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1539.pdf



The rules are in section 6.5 (Expressions) in the C specification. I don't expect you "get it" from those PDFs, but take a look and you can see what textbook writers (and programmers!) are up against when trying to make officially-correct documentation and programs.
Vaibhav
2012-09-08 07:56:05 UTC
The thing is all the decrement and increment operations will be effective when x is encountered again in the code.......



Hence right nOw the line is effectively equal to x+x+x which is equal to 6....
riquelme
2017-02-21 05:07:19 UTC
a) z = 2 * 4 = 8...after the calculation x = 3 via fact that is submit increment x++ b) z = 3 * 4 = 12....the x is incremented till now comparing the expression via fact that is pre-incremented. ++x
roger
2012-09-08 07:57:35 UTC
There is no correct answer.

You are trying to modify x three times between sequence points.

look here:

http://msdn.microsoft.com/en-us/library/azk8zbxd.aspx

Your code exhibits undefined behaviour -- any answer is possible.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...