Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does gcc warn "assuming signed overflow" in one case and not in the other
    text
    copied!<p>Consider the following code:</p> <pre><code>$ cat o.c #include &lt;stdio.h&gt; #include &lt;limits.h&gt; int absolute(int i) { int j = i &lt; 0 ? -i : i; if (j&lt;0) /* This is line 6 */ return 0; return j; } int main() { int i = 1; printf("%d %d\n", i, absolute(i)); return 0; } </code></pre> <p>Compiling it with <code>-O2</code> and <code>-Wstrict-overflow</code> produces a warning:</p> <pre><code>$ gcc -O2 -Wall -Wextra -Wstrict-overflow o.c o.c: In function ‘absolute’: o.c:6:6: warning: assuming signed overflow does not occur when simplifying comparison of absolute value and zero [-Wstrict-overflow] </code></pre> <p>Now consider the following which seems functionally equivalent to the above code:</p> <pre><code>$ cat p.c #include &lt;stdio.h&gt; #include &lt;limits.h&gt; int main() { int i = 1; int j = i &lt; 0 ? -i : i; if (j&lt;0) // Changing i to INT_MIN above and changing (j&lt;0) to (j&gt;INT_MAX) // doesn't change the behavior j=0; printf("%d %d\n", i, j); return 0; } </code></pre> <p>Compiling this with the same options doesn't result in any warnings.</p> <pre><code>$ gcc -O2 -Wall -Wextra -Wstrict-overflow p.c $ </code></pre> <p>As commented in the second code, changing the assignment to <code>i=INT_MIN;</code> and the condition to <code>(j&gt;INT_MAX)</code> doesn't warn either.</p> <p>I'm using gcc 4.7.2 on Ubuntu:</p> <pre><code>$ gcc --version gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 </code></pre> <p>I'm unable to figure the difference in the two cases. Does it have something to do with optimization or is gcc not behaving quite right here?</p>
 

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