Declaration in if statement weird behaviour (C++)
<p>I have following code:</p>

<pre><code>int X = 1;
if (int X = X)
{
    printf("%d\n", X);
    if (int X = X)
    {
       printf("%d\n", X);
    }
}
</code></pre>

<p>My expectation is X should be 1, 1. However, the output is:</p>

<pre><code>1818935350
32767
</code></pre>

<p>Anyone know what's going on here? Compiled with clang-800.0.42.1</p>

<p><strong>Edit:</strong> I tried the following code with a bit tweak, and now it behaves as I expected.</p>

<pre><code>int X = 1;
if (int Y = X)
{
    printf("%d\n", Y);
    if (int Z = X)
    {
        printf("%d\n", Z);
    }
}
</code></pre>

<p>One guess is when you use variable on RHS of a declaration inside if statement, it might not refer to the variable with the same name declared at parent scope, instead it's referring to the variable that's being defined...</p>