Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whether <em>any</em> code construct produces a warning is up to the cleverness of the compiler and the choices made by its authors.</p> <pre><code>char c=9999; </code></pre> <p><code>9999</code> is a constant expression. The compiler can determine, just by analyzing the declaration with no additional context, that it's going to overflow. (Presumably plain <code>char</code> is signed; if it's unsigned, the conversion is well defined -- but a compiler could still choose to warn about it.)</p> <pre><code>int i=9999; char c=i; </code></pre> <p>This has the same semantics, but for a compiler to warn about the initialization of <code>c</code>, it would have to know that <code>i</code> has the value <code>9999</code> (or at least a value outside the range of <code>char</code>) when it analyzes that declaration. Suppose you instead wrote:</p> <pre><code>int i = 9999; i = 42; char c = i; </code></pre> <p>Then clearly no warning would be necessary or appropriate.</p> <p>As <a href="https://stackoverflow.com/a/10503469/827263">James McNellis's answer</a> indicates, a suffiently clever compiler <em>can</em> warn about either case if it performs additional analysis of what's going to happen during the execution of the program. For some compiler, it helps to enable optimization, since the analysis required to optimize code (without breaking it) can also reveal this kind of potential run-time error.</p> <p>I'll note that this is an answer to the question you asked: why is there no warning. The answer you accepted is to the <em>implied</em> question: "I <em>want</em> a warning here; how can I enable it?". I'm not complaining, just observing.</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.
 

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