Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's undefined behaviour, of course, to pass arguments not corresponding to the format, so the language cannot tell us why the output changes. We must look at the implementation, what code it produces, and possibly the operating system too.</p> <p>My setup is different from yours,</p> <blockquote> <p>Linux 3.1.10-1.16-desktop x86_64 GNU/Linux (openSuSE 12.1)</p> </blockquote> <p>with gcc-4.6.2. But it's similar enough that it's reasonable to suspect the same mechanisms.</p> <p>Looking at the generated assembly (<code>-O3</code>, out of habit), the relevant part (<code>main</code>) is</p> <pre><code>.cfi_startproc subq $8, %rsp # adjust stack pointer .cfi_def_cfa_offset 16 movl $.LC1, %edi # move format string to edi movl $1, %eax # move 1 to eax, seems to be the number of double arguments movsd .LC0(%rip), %xmm0 # move the double to the floating point register call printf xorl %eax, %eax # clear eax (return 0) addq $8, %rsp # adjust stack pointer .cfi_def_cfa_offset 8 ret # return </code></pre> <p>If instead of the <code>double</code>, I pass an <code>int</code>, not much changes, but that significantly</p> <pre><code>movl $47, %esi # move int to esi movl $.LC0, %edi # format string xorl %eax, %eax # clear eax call printf </code></pre> <p>I have looked at the generated code for many variations of types and count of arguments passed to <code>printf</code>, and consistently, the first <code>double</code> (or promoted <code>float</code>) arguments are passed in <code>xmmN</code>, <code>N = 0, 1, 2</code>, and the integer (<code>int</code>, <code>char</code>, <code>long</code>, regardless of signedness) are passed in <code>esi</code>, <code>edx</code>, <code>ecx</code>, <code>r8d</code>, <code>r9d</code> and then the stack.</p> <p>So I venture the guess that <code>printf</code> looks for the announced <code>int</code> in <code>esi</code>, and prints whatever happens to be there.</p> <p>Whether the contents of <code>esi</code> are in any way predictable when nothing is moved there in <code>main</code>, and what they might signify, I have no idea.</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