title: Why is 0 (zero) printed without leading "0x" with C printf format "%#x"?
body: <p>Background: I have a number of scripts that parse log files looking for hex numbers by finding the leading "0x". Our embedded C library changed to a new printf. The new printf is more standards compliant than our previous and my scripts broke.</p>
  
  <p>On a Linux box:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main( void )
  {
      printf( "%#010x\n", 0 );
      printf( "%#010x\n", 1 );
      return 0;
  }
  </code></pre>
  
  <p>Output (using glibc) is:</p>
  
  <pre><code>0000000000
  0x00000001
  </code></pre>
  
  <p>Output on our firmwware was:</p>
  
  <pre><code>0x00000000
  0x00000001
  </code></pre>
  
  <p>From printf(3), on the '#' flag character:
  "For x and X conversions, a nonzero result has the string "0x"  (or "0X" for  X  conversions) prepended to it."</p>
  
  <p>I'm curious <em>why</em>. Without digging through the C standards documents or buying lunch for standards committee members, why not a leading 0x on a zero valued argument?</p>