title: How do printf and scanf handle floating point precision formats?
body: <p>Consider the following snippet of code:</p>
  
  <pre><code>float val1 = 214.20;
  double val2 = 214.20;
  
  printf("float : %f, %4.6f, %4.2f \n", val1, val1, val1);
  printf("double: %f, %4.6f, %4.2f \n", val2, val2, val2);
  </code></pre>
  
  <p>Which outputs:</p>
  
  <pre class="lang-none prettyprint-override"><code>float : 214.199997,  214.199997, 214.20 | &lt;- the correct value I wanted 
  double: 214.200000,  214.200000, 214.20 |
  </code></pre>
  
  <p>I understand that <code>214.20</code> has an infinite binary representation. The first two elements of the first line have an approximation of the intended value, but the the last one seems to have no approximation at all, and this led me to the following question:</p>
  
  <p><strong>How do the <code>scanf</code>, <code>fscanf</code>, <code>printf</code>, <code>fprintf</code> (etc.) functions treat the precision formats?</strong></p>
  
  <p>With no precision provided, <code>printf</code> printed out an approximated value, but with <code>%4.2f</code> it gave the correct result. Can you explain me the algorithm used by these functions to handle precision?</p>