title: Does i=i++ cause undefined behavior even if not executed?
body: <p>The following code appeared in <a href="https://stackoverflow.com/questions/58660861/k-expression-in-c">this question</a>:</p>
  
  <pre><code>int k = 0;
  while(+(+k--)!=0)
      k=k++;
  </code></pre>
  
  <p>The statement <code>k=k++</code> would unquestionably invoke <a href="https://en.wikipedia.org/wiki/Undefined_behavior" rel="nofollow noreferrer">undefined behavior</a> if executed because <code>k</code> is both read and written without a sequence point.  However, in the above code the body of the loop is never entered so the offending statement is not executed.</p>
  
  <p>The question is does this cause the whole program to be undefined anyway?  In a situation such as this:</p>
  
  <pre><code>if (x != NULL)
      *x = 4;
  </code></pre>
  
  <p>The statement <code>*x = 4;</code> would only cause undefined behavior if <code>x</code> is NULL.  But in the case of <code>k=k++</code> it is always undefined.  </p>
  
  <p>So the question is how would a compiler be expected to handle code such as this?  Does it automatically invalidate the entire program, even if the statement doesn't execute?</p>