I have this little code to demonstrate it:
#include "stdio.h"
int main() {
int a = testfunc(); // declared later
printf("%d", a);
return 0;
}
int testfunc() {
return 1;
}
It compiles with no error, and a outputs 1
as expected.
See in action: http://ideone.com/WRF94E
Why there is no error? Is it part of the spec of C or a compiler related thing?
The function testfunc() is implicitly declared. The compiler cannot do any signature checks so you might get a runtime error in case you fail to invoke it corectly.
This is part of the C specification. But the recommendation in the C specification is to declare all the functions you plan to implement and use, at the beginning of the current file or in a header file.
Because, by default the compiler considers the undeclared functions as
int function_name ();
Internally, an undeclared function is considered with return type as int and accepts any number of arguments.It matches with your actual function also. so no issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With