Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code you posted is invalid form the point of view of C language. The result of any cast in C is an rvalue. It cannot be used as an argument of <code>++</code>. Operator <code>++</code> requires an lvalue argument. I.e. expression <code>++((unsigned) x)</code> is non-compilable in standard C language. </p> <p>What you actually observe in this case is GCC's "generalized lvalues" extension </p> <p><a href="http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Lvalues.html" rel="nofollow">http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Lvalues.html</a></p> <p>Per that extension (and contrary to the standard C), a cast applied to an lvalue produces an lvalue. When you attempt to write something into the resultant "generalized" lvalue, the value being written is converted <em>twice</em>: it is first converted to the type specified by the explicit cast, and then the intermediate result is converted again to the type of recipient object. The final result is placed into the recipient object.</p> <p>For example, if with your <code>x</code> you do</p> <pre><code>(unsigned) x = 0x100; </code></pre> <p>it will be actually interpreted by GCC as</p> <pre><code>x = (uint8_t) (unsigned) 0x100; </code></pre> <p>and the final value of <code>x</code> will be <code>0</code>. </p> <p>And this is exactly what happens in your example. In GCC your </p> <pre><code>++((unsigned) x) </code></pre> <p>is equivalent to</p> <pre><code>(unsigned) x = (unsigned) x + 1; </code></pre> <p>which is in turn interpreted by GCC as</p> <pre><code>x = (uint8_t) (unsigned) ((unsigned) x + 1); </code></pre> <p>This is why you get <code>0</code> in <code>x</code> as the result, and that is the <code>0</code> that then gets assigned to your <code>y</code>.</p> <p>This extension is referred to as <em>deprecated</em> by GCC docs.</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