Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Any time the behavior of a program is undefined, anything can happen — the classical phrase is that "demons may fly out of your nose" — although most implementations don't go <em>that</em> far.</p> <p>The arguments of a function are conceptually evaluated in parallel (the technical term is that there is no sequence point between their evaluation). That means the expressions <code>++x</code> and <code>x+1</code> may be evaluated in this order, in the opposite order, <em>or in some interleaved way</em>. When you modify a variable and try to access its value in parallel, the behavior is undefined.</p> <p>With many implementations, the arguments are evaluated in sequence (though not always from left to right). So you're unlikely to see anything but 2 in the real world.</p> <p>However, a compiler could generate code like this:</p> <ol> <li>Load x into register <code>r1</code>. </li> <li>Calculate <code>x+1</code> by adding 1 to <code>r1</code>.</li> <li>Calculate <code>++x</code> by adding 1 to <code>r1</code>. That's ok because <code>x</code> has been loaded into <code>r1</code>. Given how the compiler was designed, step 2 cannot have modified <code>r1</code>, because that could only happen if <code>x</code> was read as well as written between two sequence points. Which is forbidden by the C standard.</li> <li>Store <code>r1</code> into <code>x</code>.</li> </ol> <p>And on this (hypothetical, but correct) compiler, the program would print 3.</p> <p>(<strong>EDIT:</strong> passing an extra argument to <code>printf</code> is correct (§7.19.6.1-2 in <a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf" rel="nofollow noreferrer">N1256</a>; thanks to <a href="https://stackoverflow.com/users/165520/prasoon-saurav">Prasoon Saurav</a>) for pointing this out. Also: added an example.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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