Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: inherits MEM02-C exception code

...

If a function declaration is not visible at the point at which a call to the function is made, C90-compliant platforms assume an implicit declaration of

extern int func();

This implies that the function may take any number and type of arguments, and returns a single int.

However, to conform with C99, you must explicitly prototype every function before invoking it. An implementation that conforms to C99 or later may or may not perform implicit function declarations. But However, C99 does require the implementation to issue a diagnostic if it encounters an undeclared function being used.

This In the following noncompliant code example, if malloc() is not declared, either explicitly, or by including stdlib.h, a compiler may implicitly declare malloc() as int malloc(). (Compilers that only comply with C90 are required to provide an implicit declaration of malloc().)  If the platform's size of int is 32 bits, but the size of pointers is 64 bits, the resulting pointer could be truncated as a result of the implicit declaration of malloc() returning a 32-bit integer.

/* #include <stdlib.h> is missing */
 
int main(void) {
  size_t i;
  for (i = 0; i < 100; ++i) {
    char *ptr = (char*)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return 0;
}

When compiled with Microsoft Visual Studio (a C90-only platform), the above code will eventually cause an access violation when dereferencing ptr in the loop.

Compliant Solution (Implicit Function Declaration)

In this compliant solution, malloc() is explicitly declared before it is used. example fails to prototype the foo() function before invoking it in main():

Code Block
bgColor#FFCCCC#ccccff
langc
void *malloc(size_t size);
 
int main(void) {
  size_t i;
  intfor c(i = foo();
  printf("%d\n", c);
  return 0;
}

int foo(int a) {0; i < 100; ++i) {
    char *ptr = (char*)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return a0;
}

For more information on function declarations, see DCL07-C. Include the appropriate type information in function declaratorsBecause the compiler assumes foo() to have type extern int foo(), it cannot diagnose the missing argument.

Compliant Solution (Implicit Function Declaration)

In this This compliant solution , a prototype for foodeclares malloc() appears before the function invocation: by including the appropriate header file.

Code Block
bgColor#ccccff
langc
int foo(int);
#include <stdlib.h>
 
int main(void) {
  size_t i;
  intfor c(i = foo(0);
 i  printf("%d\n", c);
  return 0;
}

int foo(int a) {< 100; ++i) {
    char *ptr = (char*)malloc(0x10000000); /* int malloc() assumed */
    *ptr = 'a';
  }
  return a0;
}

...

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.

...