title: Representation of float in C
body: <p>I was trying to understand the floating point representation in C using this code (both <code>float</code> and <code>int</code> are 4 bytes on my machine):</p>
  
  <pre><code>int x = 3;
  float y = *(float*) &amp;x;
  printf("%d %e \n", x, y);
  </code></pre>
  
  <p>We know that the binary representation of x will be the following</p>
  
  <p><code>00000000000000000000000000000011</code></p>
  
  <p>Therefore I would have expected y to be represented as follows</p>
  
  <ul>
  <li><p>Sign bit (first bit from left) = <code>0</code></p></li>
  <li><p>Exponent (bits 2-9 from left) = <code>0</code></p></li>
  <li><p>Mantissa (bits 10-32): <code>1 + 2^(-22)+2^(-23)</code></p></li>
  </ul>
  
  <p>Leading to <code>y = (-1)^0 * 2^(0-127) * (1+2^(-22) + 2^(-23)) = 5.87747E-39</code></p>
  
  <p>My program however prints out</p>
  
  <p><code>3 4.203895e-45</code></p>
  
  <p>That is, y has the value <code>4.203895e-45</code> instead of <code>5.87747E-39</code> as I expected. Why does this happen. What am I doing wrong?</p>
  
  <p>P.S. I have also printed the values directly from gdb so it is not a problem with the printf command.</p>