title: C++: What is the printf() format spec for "float"?
body: <p>C++: What is the <code>printf()</code> format spec for <code>float</code>? (Visual C++)</p>
  
  <p>It used to be that I used <code>%g</code> for <code>float</code> and <code>%lg</code> for <code>double</code>. </p>
  
  <p>It looks like the spec changed and <code>float</code> is undefined and <code>double</code> is <code>%g</code>.</p>
  
  <p>I have bits in memory that I am printing out so casting is not an option.</p>
  
  <p>Is there a way that I can print out <code>float</code> values using <code>printf()</code> ?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This code was written for unit testing generic C++ libs used on an embedded system. 
  Here's what I had to do to get the <code>float</code> to work. 
  The code is in a template function:</p>
  
  <pre><code>template &lt;typename T,typename TTYP,typename Ttyp,int bits,bool IsSigned&gt; 
  Error testMatrixT() 
  { ...
  </code></pre>
  
  <p>Here is a code snip:</p>
  
  <pre><code>if (typeid(Ttyp) == typeid(float)) {    
      float64 c = *(float32*)&amp;Tp(row,col);
      float64 a1 = *(float32*)&amp;Arg1(row,col);
      float64 a2 = *(float32*)&amp;Arg2(row,col);
      float64 e = *(float32*)&amp;Exp(row,col);
      m_b = (c == e);
      _snprintf(m_acDiag, sizeof(m_acDiag)-1
          , "add(Arg1,Arg2): arg1=%g, arg2=%g, Expected=%g, Actual=%g, Result: %s"
          , a1, a2, e, c, BOOL_PF(m_b));
  } else {
      ...
  </code></pre>
  
  <p>Pretty ugly isn't it? Using floats as args give bad output. Maybe due to using <code>_snprintf()</code> ?
  Years ago I would use <code>%lg</code> and it would be OK. Not anymore.</p>