...
Most C90 implementations do not issue a diagnostic for the violation of this C99 constraint. Many C99 translators will continue to treat such declarations as implying the type int
.
Compliant Solution
This compliant solution explicitly declares foo
to be of type int
.
Code Block | ||
---|---|---|
| ||
extern int foo; |
...
extern int func();
However, for conformance to conform with C99, you should must explicitly prototype every function before making a call to itinvoking it. This non-compliant example fails to prototype the foo()
function before invoking it in main()
.
Code Block | ||
---|---|---|
| ||
int main(void) { int c = foo(); printf("%d\n", c); return 0; } int foo(int a) { return a; } |
Because the compiler assumes foo()
to have type extern int foo()
, it cannot diagnose the missing argument and a bogus value is printed.
Compliant Solution (implicit function declaration)
In this compliant solution, a prototype for foo()
appears before the function invocation.
Code Block | ||
---|---|---|
| ||
int foo(int); int main(void) { int c = foo(0); printf("%d\n", c); return 0; } int foo(int a) { return a; } |
...
Non-Compliant Code Example (implicit return type)
Similarly, don't do not declare a function with implicit return type. If it returns a meaningful integer value, declare it int. If it returns no meaningful value, declare it void.
...