...
This compliant solution explicitly declares foo
to be of type int
includes a type specifier.
Code Block | ||
---|---|---|
| ||
extern int foo; |
Noncompliant Code Example (
...
Implicit Function Declaration)
Implicit declaration of functions is not allowed: every function must be explicitly declared before it can be called. In C89, if a function is called without an explicit prototype, the compiler provides an implicit declaration.
The C90 Standard included includes the requirement:
If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration
extern int identifier();
appeared.
...
Because the compiler assumes foo()
to have type extern int foo()
, it cannot diagnose the missing argument.
Compliant Solution (
...
Implicit Function Declaration)
In this compliant solution, a prototype for foo()
appears before the function invocation.
...
For more information on function declarations see DCL07-C. Include the appropriate type information in function declarators.
Noncompliant Code Example (
...
Implicit Return Type)
Similarly, 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.
...
Because the compiler assumes that foo()
returns a value of type int
, UINT_MAX
is incorrectly converted to -1.
Compliant Solution (
...
Implicit Return Type)
This compliant solution explicily defines the return type of foo()
as unsigned int.
Code Block | ||
---|---|---|
| ||
unsigned int foo(void) { return UINT_MAX; } int main(void) { long long c = foo(); printf("%lld\n", c); return 0; } |
...
Occurrences of an omitted type specifier in existing code are rare, and the consequences are generally minor, at worst usually causing perhaps resulting in abnormal program termination.
...