Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The two lines:</p> <pre><code>#define SQUARE(x) (x*x) j=SQUARE(i++); </code></pre> <p>translates into:</p> <pre><code>j = (i++ * i++); </code></pre> <p>which is undefined behaviour. You are not permitted to modify a variable twice without an intervening sequence point (and <code>*</code> is not a sequence point).</p> <p>You're better off using something like:</p> <pre><code>inline int SQUARE (int x) { return x * x; } </code></pre> <p>What's probably happening is that the increments are happening together after or before the multiplication is done, effectively giving you:</p> <pre><code>i = 3; // i = 3 j = i * i; i++; i++; // j = 9, i = 5 ++i; ++i; k = i * i; // i = 7, k = 49 </code></pre> <p>But keep in mind that's what's happening in <em>this case.</em> An implementation is free to do it some other way since you're breaking the rules. In fact, it can format your hard disk if it wants to. That is the nature of undefined behaviour, defined as (my italics):</p> <blockquote> <p>behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes <em>no</em> requirements.</p> <p>NOTE Possible undefined behavior ranges from ignoring the situation completely <em>with unpredictable results,</em> to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).</p> </blockquote>
 

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