title: Why doesn't linking a function of the wrong type return garbage?
body: <p>I'm going through the K&amp;R C book and chapter 4.2 says the following:</p>
  <blockquote>
  <p>If atof itself and the call to it in main have inconsistent types in the same source file, the error will be detected by the compiler. But if (as is more likely) atof were compiled separately, the mismatch would not be detected, atof would return a double that main would treat as an int, and meaningless answers would result.</p>
  </blockquote>
  <p>I wanted to see for myself what would happen if I created and inconsistently used a function as they describe:</p>
  <p>main.c:</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int floatFunction();
  
  int main() {
      int result = floatFunction();
      printf(&quot;result: %d\n&quot;, result);
  }
  </code></pre>
  <p>floatFunction.c:</p>
  <pre><code>float floatFunction() {
      return 10;
  }
  </code></pre>
  <p>bash:</p>
  <pre><code>$ gcc main.c floatFunction.c -o main
  $ ./main
  result: 0
  $
  </code></pre>
  <p>Regardless of what value I had <code>floatFunction</code> return, I always got <code>0</code> as a result. I also tried compiling with <code>-o3</code>, same result. I was expecting to see the bit pattern of <code>10.0</code> interpreted as an int as a result. Why is this not happening?</p>
  <p>Also, why doesn't the compilation process notify me that the types don't match and how can I protect myself from making a mistake like this in real code?</p>