Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed for C11, fixed several formatting issues.

...

Noncompliant Code Example (Implicit int)

C no longer allows for the absence of type specifiers in a declaration. In these cases, the type is defined to be that of a signed int.Do not rely on implicit int typing.  Subclause 6.7.2 of the C Standard [ISO/IEC 9899:2011] states:

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name.

...

Code Block
bgColor#FFCCCC
langc
extern foo;

Most Some C implementations do not issue a diagnostic for the violation of this constraint. Many These noncompliant C translators continue to treat such declarations as implying the type int.

...

Implicit declaration of functions is not allowed: every function should be explicitly declared before it can be called. In C89C90, if a function is called without an explicit prototype, the compiler provides an implicit declaration.

...

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.

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 C99the C Standard, you must explicitly prototype every function before invoking it. An  An implementation that conforms to C99 or later the C Standard may or may not perform implicit function declarations. However, C99 does C does require the implementation to issue a diagnostic if it encounters an undeclared function being used.

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 comply only 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.

 

Code Block
bgColor#ffcccc
langc
/* #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 preceding code will eventually cause an access violation when dereferencing ptr in the loop.

Compliant Solution (Implicit Function Declaration)

...