title: Why does C print my hex values incorrectly?
body: <p>So I'm a bit of a newbie to C and I am curious to figure out why I am getting this unusual behavior.</p>
  
  <p>I am reading a file 16 bits at a time and just printing them out as follows.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define endian(hex) (((hex &amp; 0x00ff) &lt;&lt; 8) + ((hex &amp; 0xff00) &gt;&gt; 8))
  
  int main(int argc, char *argv[])
   {
    const int SIZE = 2;
    const int NMEMB = 1;
    FILE *ifp; //input file pointe
    FILE *ofp; // output file pointer
  
    int i;
    short hex;
    for (i = 2; i &lt; argc; i++)
     {
      // Reads the header and stores the bits
      ifp = fopen(argv[i], "r");
      if (!ifp) return 1;
      while (fread(&amp;hex, SIZE, NMEMB, ifp))
       {
        printf("\n%x", hex);
        printf("\n%x", endian(hex)); // this prints what I expect
        printf("\n%x", hex);
        hex = endian(hex);
        printf("\n%x", hex);
       }   
     }
   }
  </code></pre>
  
  <p>The results look something like this:</p>
  
  <pre><code>ffffdeca
  cade // expected
  ffffdeca
  ffffcade
  0
  0 // expected
  0
  0
  600
  6 // expected
  600
  6
  </code></pre>
  
  <p>Can anyone explain to me why the <em>last</em> line in each block doesn't print the same value as the second?</p>