Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry for the first answer, here is an explanation from the C++ standards :)</p> <blockquote> <p>Is the output in any way predictable or its undefined??</p> </blockquote> <p>It is predictable. There are two points to look after in this code: First, the assignment of value that the type <code>unsigned char</code> can't hold:</p> <pre><code>unsigned char c; c = 300; </code></pre> <blockquote> <p><strong>3.9.1 Fundamental types (Page 54)</strong> </p> <p>Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.41)<br> ...<br> 41) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.</p> </blockquote> <p>Basically:</p> <pre><code>c = 300 % (std::numeric_limits&lt;unsigned char&gt;::max() + 1); </code></pre> <p>Second, passing <code>%d</code> in the format string of <code>printf</code> to print <code>unsigned char</code> variable.<br> This one <a href="https://stackoverflow.com/users/17389/ysth">ysth</a> got it right ;) There is no undefined behavior, because a promotional conversion from unsigned char to int happens in the case of <code>variadic arguments</code>!</p> <p>Note: that the second part of the answer is a <strong>rephrasing</strong> of what have been said in <a href="https://stackoverflow.com/questions/2151305/gcc-warning-large-integer-implicitly-truncated-to-unsigned-type/2151340#2151340">the comments of this answer</a> but it is not my answer originally.</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