Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>printf("%d\b",x) </code></pre> <p>prints the characters <code>'1'</code>, <code>'0'</code> (because x==10) and <code>\b</code>. The <code>\b</code> is a backspace character; if you print to a terminal, it will print <code>10</code> and then move the cursor back one column.</p> <p>A call to <code>printf</code> returns the number of characters it printed; in this case, the result is 3 (yes, <code>'\b'</code> counts as a character).</p> <pre><code>printf("%d\n",printf("%d\b",x)); </code></pre> <p>The inner <code>printf</code> call works as I explained above, and returns 3. The outer <code>printf</code> call prints <code>"3\n"</code>.</p> <p>So the entire statement will print:</p> <pre><code>10\b3\n </code></pre> <p>The <code>'\b'</code> causes the <code>3</code> to be replace the <code>0</code> on the screen, so the final displayed result (when I run the program on my system) is:</p> <pre><code>13 </code></pre> <p>If I pipe the output through <code>cat -v</code>, I get:</p> <pre><code>10^H3 </code></pre> <p>where <code>^H</code> represents the backspace character.</p> <p><strong>EDIT :</strong></p> <p>The question was just edited, and the modified program's behavior is quite different. The switch statement causes control to jump <em>past</em> the declaration <code>int x = 10;</code>, but into the scope in which <code>x</code> is declared. As a result, <code>x</code> is uninitialized when <code>printf</code> is called. This causes undefined behavior, and most likely garbage output (I just got <code>-1217572876^H12</code>). If <code>x</code> happens to be <code>0</code>, I suppose you'd get <code>0^H2</code>, which would look like <code>2</code>.</p> <p>Whatever you're trying to do, please find a better way to do it.</p>
 

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