title: Why floating point comparisons gives different outputs on different compiler?
body: <p>I was reading <a href="http://www.geeksforgeeks.org/comparison-float-value-c/" rel="nofollow">this</a>. It contains following C program.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main()
  {
      float x = 0.1;
      if (x == 0.1)
          printf("IF");
      else if (x == 0.1f)
          printf("ELSE IF");
      else
          printf("ELSE");
  }
  </code></pre>
  
  <p>The article says that</p>
  
  <blockquote>
    <p>The output of above program is “ELSE IF” which means the expression “x
    == 0.1″ returns false and expression “x == 0.1f” returns true.</p>
  </blockquote>
  
  <p>But I tried it on different compilers &amp; getting different outputs:</p>
  
  <p>Here is the outputs on various IDEs.</p>
  
  <p>1) Orwell Dev C++: ELSE </p>
  
  <p>2) Code Blocks 13.12: ELSE IF but it gives following warnings during compilation.</p>
  
  <blockquote>
    <p>warning: comparing floating point with == or != is unsafe.</p>
  </blockquote>
  
  <p>Why this comparison is unsafe?</p>
  
  <p>3) Ideone.com: ELSE IF (see run: <a href="http://ideone.com/VOE3E0" rel="nofollow">http://ideone.com/VOE3E0</a>)</p>
  
  <p>4) TDM GCC 32 bit: ELSE IF</p>
  
  <p>5) MSVS 2010: ELSE IF but compiles with warning</p>
  
  <blockquote>
    <p>Warning   1   warning C4305: 'initializing' : truncation from 'double' to
    'float'</p>
  </blockquote>
  
  <p>What is exactly happening here? What's wrong with the program? Is it implementation defined behavior occurring?</p>
  
  <p>Please help me.</p>