Note that there are some explanatory texts on larger screens.

plurals
  1. POGCC, weird integer promotion scheme
    primarykey
    data
    text
    <p>I'm working with GCC v4.4.5 and I've notived a default integer promotion scheme I didn't expected.</p> <p>To activate enough warnings to prevent implicit bugs I activated the option -Wconversion and since then I've notice that when I perform the code below, the warning <em>"conversion to ‘short int’ from ‘int’ may alter its value"</em> is present.</p> <pre><code>signed short sA, sB=1, sC=2; sA = sB + sC; </code></pre> <p>This means that <em>"sB + sC"</em> is promoted to <em>int</em> and then assigned to <em>sA</em> which is <em>signed short</em>. To fix this warning I have to cast it like this.</p> <pre><code>signed short sA, sB=1, sC=2; sA = ( signed short )( sB + sC ); </code></pre> <p>This warning is also present with the code below.</p> <pre><code>signed short sA=2; sA += 5; </code></pre> <p>And can be fixed by removing the operator <em>+=</em> by this...</p> <pre><code>sA = ( signed short )( sA + 1 ); </code></pre> <p>which is a bit annoying cause I can't use the operators <em>+=</em>, <em>-=</em>. </p> <p>I expected GCC to select the right integer promotion according to the operands. I mean, <em>sA=sB+sC</em> and <em>sA+=5</em> should not be promoted to <em>int</em> as they all are <em>signed short</em>.</p> <p>I understand that promoting to <em>int</em> by default prevents overflow bugs but it's a bit annoying cause I have to cast most of my code or change my variables to <em>int</em>.</p> <p>Is there a GCC option I could use to present this integer promotion scheme ?</p> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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