Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich of the following combinations of post & pre-increment operators have undefined behaviour in C?
    primarykey
    data
    text
    <p>I've read, <a href="https://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc">Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...)</a> and <em>tried understanding</em> <a href="http://c-faq.com/expr/seqpoints.html" rel="nofollow noreferrer">Sequence points on "comp.lang.c FAQ"</a> after wasting more than 2 hours of time trying to explain the following results by gcc compiler.</p> <pre><code>expression(i=1;j=2) i j k k = i++ + j++; 2 3 3 k = i++ + ++j; 2 3 4 k = ++i + j++; 2 3 4 k = ++i + ++j; 2 3 5 k = i++ + i++; 3 2 k = i++ + ++i; 3 4 k = ++i + i++; 3 4 k = ++i + ++i; 3 6 i = i++ + j++; 4 3 i = i++ + ++j; 5 3 i = ++i + j++; 4 3 i = ++i + ++j; 5 3 i = i++ + i++; 4 i = i++ + ++i; 5 i = ++i + i++; 5 i = ++i + ++i; 6 </code></pre> <p><strong>Question:</strong></p> <ol> <li><p>I want to know if all the expressions shown (in 4 groups) in above figure have undefined behavior? If only some of them have undefined behavior which ones does and which ones doesn't? </p></li> <li><p>For defined behaviour expressions, kindly can you <em>show (not explain)</em> how compiler evaluates them. Just to make sure, if I got this pre-increment &amp; post increment correctly.</p></li> </ol> <p><strong>Background:</strong></p> <p>Today, I've attended a campus interview, in which I was asked to explain the results of <code>i++ + ++i</code> for a given value of <code>i</code>. After compiling that expression in gcc, I realized that the answer I gave in interview was wrong. I decided not to make such mistake in future and hence, tried to compile <em>all possible combinations</em> of pre and post increment operators and compile them in gcc and then try to explain the results. I struggled for more than 2 hours. I couldn't find single behaviour of evaluation of these expressions. So, I gave up and turned to stackoverflow. After little bit of reading archives, found that there is something like <code>sequence point</code> and undefined behaviour.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload