Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Summary: The result is implementation-defined and very likely to be <code>-1</code>, but it's complicated, at least in principle.</p> <p>The rules regarding overflow are different for operators vs. conversions, and for signed types vs. unsigned types -- and the conversion rules changed between C90 and C99.</p> <p>As of C90, overflow of an operator with signed integer operands ("overflow" meaning that the mathematical result cannot be represented in the expression's type) has undefined behavior. For unsigned integer operands, the behavior is well defined as the usual wraparound (strictly speaking the standard doesn't call this an "overflow"). But your declaration:</p> <pre><code>char foo = 255; </code></pre> <p>doesn't use any operators (the <code>=</code> is an initializer, not an assignment), so none of that applies in this case.</p> <p>If type <code>char</code> can represent the value <code>255</code> (which is true either of plain <code>char</code> is unsigned or if <code>CHAR_BIT &gt;= 9</code>), then of course the behavior is well defined. The <code>int</code> expression <code>255</code> is implicitly converted to <code>char</code>. (Since <code>CHAR_BIT &gt;= 8</code>, it's not possible for this particular case to invoke unsigned wraparound.)</p> <p>Otherwise, the conversion yields a result that can't be stored in a <code>char</code>.</p> <p>As of C90, the result of the conversion is implementation-defined -- which means that it's guaranteed to set <code>foo</code> to <em>some</em> value within the range of type <code>char</code>, and you can determine what that value is by reading the implementation's documentation, which is required to tell you how the conversion works. (I've never seen an implementation where the stored value is anything other than <code>-1</code>, but any result is possible in principle.)</p> <p>C99 changed the definition, so that an overflowing conversion to a signed type <em>either</em> yields an implementation-defined result <em>or</em> raises an implementation-defined signal.</p> <p>If a compiler chooses to do the latter, then it must document which signal is raised.</p> <p>So what happens if an implementation-defined signal is raised? Section 7.14 of the standard says:</p> <blockquote> <p>The complete set of signals, their semantics, and their default handling is implementation-defined</p> </blockquote> <p>It's not entirely clear (to me) what the range of possible behaviors for the "default handling" of signals is. In the worst case, I suppose such a signal could terminate the program. You <em>might or might not</em> be able to define a signal handler that catches the signal.</p> <p>7.14 also says:</p> <blockquote> <p>If and when the function returns, if the value of sig is <strong>SIGFPE</strong>, <strong>SIGILL</strong>, <strong>SIGSEGV</strong>, or any other implementation-defined value corresponding to a computational exception, the behavior is undefined; otherwise the program will resume execution at the point it was interrupted.</p> </blockquote> <p>but I don't <em>think</em> that applies, since an overflowing conversion is not a "computational exception" as the term is used here. (Unless the implementation-defined signal happens to be <code>SIGFPE</code>, <code>SIGILL</code>, or <code>SIGSEGV</code> -- but that would be silly).</p> <p>So ultimately, if an implementation chooses to raise a signal in response to an overflowing conversion, the behavior (not just the result) is at least implementation-defined, and there might be circumstances in which it could be undefined. In any case, there doesn't seem to be any <em>portable</em> way to deal with such a signal.</p> <p>In practice, I've never heard of an implementation that takes advantage of the new wording in C99. For all compilers I've heard of, the result of the conversion is implementation-defined -- and very probably yields what you'd expect from a 2's-complement truncation. (And I'm not at all convinced that this change in C99 was a good idea. If nothing else, it made this answer about 3 times as long as it would otherwise have needed to be.)</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. 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