Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added code samples

...

bgColor#FFCCCC

If a function declaration is not visible at the point at which a call to the function is made, some compilers assume an implicit declaration of extern int func(); However, for conformance to C99, you should explicitly prototype every function before making a call to it.

Code Block
bgColor#FFCCCC

int main(void) {
    int c = foo();
    printf("%d\n", c);
    return 0;
}

int foo(int a) {
  return a;
}

Since the compiler assumes foo() to have type extern int func(), it cannot spot the missing argument and a bogus value is printed.

Compliant Solution (implicit function declaration)

Code Block
bgColor#ccccff
extern int foo;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 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.

Code Block
bgColor#ffcccc

foo() {
  return UINT_MAX;
}

int main(void) {
  long long c = foo();
  printf("%lld\n", c);
  return 0;
}

Since the compiler assumes that foo() returns a value of type int, UINT_MAX is incorrectly converted to -1.

Compliant Solution (implicit return type)

Code Block
bgColor#ccccff

unsigned int foo() {
  return UINT_MAX;
}

int main(void) {
  long long c = foo();
  printf("%lld\n", c);
  return 0;
}

Risk Assessment

Occurrences of an omitted type specifier in existing code is rare, and the consequences are generally minor.

...

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] 
\[[Jones 08|AA. C References#Jones 08]\]
\[[MISRA 04|AA. C References#MISRA 04]\] Section 6.7.2, "Type specifiers", Section  6.5.2.2, "Function calls"

...