I'm going through the K&R C book and chapter 4.2 says the following:
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.
I wanted to see for myself what would happen if I created and inconsistently used a function as they describe:
main.c:
#include <stdio.h>
int floatFunction();
int main() {
int result = floatFunction();
printf("result: %d\n", result);
}
floatFunction.c:
float floatFunction() {
return 10;
}
bash:
$ gcc main.c floatFunction.c -o main
$ ./main
result: 0
$
Regardless of what value I had floatFunction return, I always got 0 as a result. I also tried compiling with -o3, same result. I was expecting to see the bit pattern of 10.0 interpreted as an int as a result. Why is this not happening?
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?