Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>  

using namespace std;

int main()
{
  int x = 5, y = 5, z;
  x = ++x; y = --y;
  z = x + ++x;
  cout << z;
        return 0;
}


What I have tried:

I tried the output but coming 14 How it works please explain step by stemp
Posted
Updated 2-May-16 11:06am
v2
Comments
[no name] 2-May-16 22:08pm    
hi nv3;

I've deleted my solution. Here's the reasoning.

Originally, I thought - if this was the post-fix "++" operator, I could agree.

http://en.cppreference.com/w/cpp/language/eval_order

The "Undefined behavior" section lists the kind of cases you likely have in mind. Nearly all examples given involve the postfix operator.

The following statements are telling:

"1) If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined."

"1) Between the previous and next sequence point a scalar object must have its stored value modified at most once by the evaluation of an expression, otherwise the behavior is undefined."

The expression "x + ++x" modifies x only once (with exactly one side effect) so the statements apply. Therefore, the expression in question *is* defined.

In honesty, after reading your comment I had to do some googling. I appreciate the feedback. I would definitely not allow such code in a code review.

So ... to kill my own solution (quoted from the same link above):

"If A is not sequenced before B and B is not sequenced before A, then two possibilities exist:

evaluations of A and B are unsequenced: they may be performed in any order and may overlap (within a single thread of execution, the compiler may interleave the CPU instructions that comprise A and B)
evaluations of A and B are indeterminably-sequenced: they may be performed in any order but may not overlap: either A will be complete before B, or B will be complete before A. The order may be the opposite the next time the same expression is evaluated."

Dang. What I thought was C++ was really just my compiler.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

This line is in the gray zone: it is not possible to know the result as it depend on the compiler internals.
C++
z = x + ++x;
 
Share this answer
 
C++
#include <stdio.h>  

using namespace std;
 
int main()
{
  // Declaring and initializing (e.g. assigning values) two variables x and y
  // Also declaring the variable z without initialization
  int x = 5, y = 5, z;
  // Using prefix increment and decrement operators to change the
  // value of x and y variables. Normally you don't need to perform
  // x = ++x; and y = --y, because the operation on 'x' and 'y' might 
  // not be undefined because of the operator precedence violation; 
  // Instead, just do as follows: ++x; --y;
  x = ++x; y = --y;
  // Here the value of x is incremented using prefix increment operator
  // but, again, we've got the precedence violation at this point.
  // Instead, we have to do as follows: z = x + (x + 1);
  z = x + ++x;
  cout << z;
        return 0;
}


Here's the complete corrected code that performs the described computation:

C++
#include <stdio.h>
#include <iostream>

using namespace std;
 
int main()
{
  int x = 5, y = 5, z;
  ++x; --y;
  z = x + (x + 1);
  cout << z;
        return 0;
}


The result is 13. (http://codepad.org/n9uSsw2Z[^])

Is that what you've actually needed ? Just post your comment.
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 1-May-16 7:03am    
You are assuming that 13 is the correct answer, and that because of the white-spaces in the original code...However, it is a pure case of a question, where OP just jumped into C++ development, without solid foundation...So IMHO, there is no 'right' solution, but OP to learn the basics...
I also amazed by the phrase 'operator precedence violation'...What exactly the code violates?
Arthur V. Ratz 1-May-16 7:12am    
1. "You are assuming that 13 is the correct answer...", if not, so I wondering what the correct answer actually is ?
2. "I also amazed by the phrase 'operator precedence violation'...What exactly the code violates?", normally this code doesn't violate anything, by this phrase I actually meant that there's multiple operator precedence violation such as x = ++x; y = --y; or z = x + ++x;, which normally causes on 'x' and 'y' might not be undefined serious warning as well as obtaining the incorrect results while performing the computation.
3. Finally, the correct code in this particular case is similar in both C and C++, since there's no way to use prefix increment in z = x + ++x;

That's it.
Kornfeld Eliyahu Peter 1-May-16 7:15am    
1. What I'm trying to tell, that 14 IS the answer, and the only problem with this code is the lack of understanding of OP...
2. See (1) :-)
Arthur V. Ratz 1-May-16 7:18am    
Agree that, the following code is not the correct one since the multiple warnings telling that 'x' and 'y' might not be undefined. You can make sure that the following code's correct output is 13 (e.g. 6 + 7 = 6 + (6 + 1) by running this code here: http://codepad.org/n9uSsw2Z
Kornfeld Eliyahu Peter 1-May-16 7:25am    
You are missing the point...YOUR code will output 13, but the original code will output 14 (and without any warning!!!)...The code has no problem whatsoever (it compiles and does what written there, exactly), but OP has, as OP does not understand (know?) the very basics of C++ language...My point is, that the only way to fix that code is teach OP the basics, so OP will understand what written there...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900