title: Dumping bits/bytes to a file in binary mode
body: <p>I have below code.</p>
  
  <pre><code>FILE *fp;
  int a;
  fp=fopen("dump.bin","wb");         
  a = 0xffafbcdf;
  fprintf(fp,"%x",a&amp;0x3ffff);
  </code></pre>
  
  <p>I am trying to dump only 18 LSBits of variable a. But the value dumped in the file is 3bcdf.</p>
  
  <p>My question - Is it not possible to dump/fwrite/fprintf desired number of bits which is not a multiple of 4(nibble) to a file? </p>
  
  <p>EDIT:</p>
  
  <p>1.) When I checked the output of my file dump I realized, since I am opening file in binary mode, I should not be using <code>fprintf</code>, but I should use <code>fwrite</code>.</p>
  
  <p>2.) What I see in the output is <code>DF BC 03</code> , it writes in multiples of 8 bits, so it writes 24 bits(3 bytes) but i was interested in only 18 bits. But then I realized that no  file write library will be able to write non-multiple of 8 bits to a file. it will always add leading zero bits to complete the byte and then dump it. "Least count unit"  for a file data is a byte.</p>