title: Casting between different datatypes in C++
body: <p>Working on casting between different datatypes in C++... The program here below prints: </p>
  
  <pre><code>&gt;"Number is 2"
  &gt;"Number is 2.5"
  &gt;"Number is 2" 
  </code></pre>
  
  <p>Please, explain why the last printout is not "Number is 2.5" which I would expect after the C++ style cast to float?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  using namespace std;
  
  int main() {
  
      int iNumber = 5;
      float fNumber;
  
      // No casting - C++ implicitly converts the result into an int and saves into a float
      // which is a conversion from 'int' to 'float' with possible loss of data
      fNumber = iNumber / 2;
      cout &lt;&lt; "Number is " &lt;&lt; fNumber &lt;&lt; endl;
  
      // C-style casting not recommended as not type safe
      fNumber = (float) iNumber / 2;
      cout &lt;&lt; "Number is " &lt;&lt; fNumber &lt;&lt; endl;
  
      // C++ style casting using datatype constructors to make the casting safe
      fNumber = static_cast&lt;float&gt;(iNumber / 2);
      cout &lt;&lt; "Number is " &lt;&lt; fNumber &lt;&lt; endl;
  
     _getch();
  
     return 0;
  }
  </code></pre>