title: Undefined behavior in c/c++: i++ + ++i vs ++i + i++
body: <p>Imagine that we have the code below:<br></p>
  
  <pre><code>int i = 1;
  int j = i++ + ++i;
  </code></pre>
  
  <p>I know that this is a Undefined Behavior, because before the semicolon, which is a sequence point, the value of <code>i</code> has been changed more than once. It means that the compiler may have two possibilities even if the precedence of operator plus is Left-to-Right:<br></p>
  
  <p>case 1)</p>
  
  <ol>
  <li>take the value of <code>i++</code> --- value of <code>i</code> is 1</li>
  <li>take the value of <code>++i</code> --- value of <code>i</code> is 2</li>
  <li>do the operator plus and assign the result which is 3 to <code>j</code> and do the side effect of <code>i++</code> (the order of this step is undefined too but we don't care because it won't change the result)</li>
  </ol>
  
  <p>case 2)</p>
  
  <ol>
  <li>take the value of <code>i++</code> --- value of <code>i</code> is 1</li>
  <li>do the side effect of <code>i++</code> --- value of <code>i</code> is 2</li>
  <li>take the value of <code>++i</code> --- current value of <code>i</code> is 3</li>
  <li>do the operator plus and assign the result which is 4 to <code>j</code></li>
  </ol>
  
  <p>If nothing is wrong here, I have a question:<br></p>
  
  <p><code>int j = ++i + i++;</code></p>
  
  <p>Is the code above still an Undefined Behavior?<br></p>
  
  <p>In my opinion, there is only one possibility:<br></p>
  
  <ol>
  <li>do the side effect of <code>++i</code> --- value of <code>i</code> is 2</li>
  <li>take the value of <code>i++</code> --- value of <code>i</code> is 2</li>
  <li>do the operator plus and assign the result which is 4 to <code>j</code> and do the side effect of <code>i++</code> (the order of this step is undefined too but we don't care because it won't change the result)</li>
  </ol>
  
  <p>Am I right?</p>
  
  <p>Btw I've read this link:<br>
  <a href="https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points/4183735#4183735">Undefined behavior and sequence points</a></p>