Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>floats</code> are automatically promoted to <code>doubles</code> when passed as <code>...</code> parameters (similarly to how <code>chars</code> and <code>short ints</code> are promoted to <code>ints</code>). When <code>printf()</code> looks at a format specifier (<code>%d</code> or <code>%f</code> or whatever), it grabs and interprets the raw data it has received according to the format specifier (as <code>int</code> or <code>double</code> or whatever) and then prints it.</p> <p><code>printf("%d\n",x)</code> is wrong because you are passing a <code>double</code> to <code>printf()</code> but lie to it that it's going to be an <code>int</code>. <code>printf()</code> makes you pay for the lie. It takes 4 bytes (most likely, but not necessarily 4 bytes) from its parameters, which is the size of an <code>int</code>, it grabs those bytes from where you have previously put 8 bytes (again, most likely, but not necessarily 8 bytes) of the <code>double</code> 4.0, of which those 4 bytes happen to be all zeroes and then it interprets them as an integer and prints it. Indeed, powers of 2 in the IEEE-754 double precision format normally have 52 zero bits, or more than 6 8-bit bytes that are zero.</p> <p>Those "most likely, but not necessarily" words mean that the C standard does not mandate a fixed size and range for types and they may vary from compiler to compiler, from OS to OS. These days 4-byte <code>ints</code> and 8-byte <code>doubles</code> are the most common (if we consider, e.g. the x86 platform).</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