Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The C Standard does not allow for implicit typing of variables and functions. The C90 Standard did allow such implicit typing.   Consequently, there exists some legacy code that uses implicit typing.   Some C compilers still support legacy code by allowing implicit typing, but it should not be used for new code.   Because implicit declarations lead to less stringent type checking, they can introduce unexpected and erroneous behavior or even security vulnerabilities.

The C Standard requires type identifiers and forbids implicit function declarations. After issuing the diagnostic, an implementation may choose to assume an implicit declaration and continue translation to support existing programs that used this feature.

...

Do not rely on implicit int typing. Section Subclause 6.7.2 of the C Standard [ISO/IEC 9899:2011] states:

...

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

...

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. However, C99 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 comply with C90 are required to provide an implicit declaration of malloc().)  If 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 above preceding code will eventually cause an access violation when dereferencing ptr in the loop.

...

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

 

For more information on function declarations, see DCL07-C. Include the appropriate type information in function declarators.

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

 

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.DCL31

Fully implemented

GCC

Include Page
GCC_V
GCC_V

 

Can detect violations of this rule when the -Wimplicit and -Wreturn-type flags are used

Klocwork

Include Page
Klocwork_V
Klocwork_V

IF_MISS_DECL RETVOID.IMPLICIT

 

LDRA tool suite

Include Page
LDRA_V
LDRA_V

24 D
20 S
326 S

Fully implemented

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0434 (C)
1302
2050
2051
3335

Fully implemented

...

Bibliography

[ISO/IEC 9899:2011]Section Subclause 6.7.2, "Type Specifiers"
[Jones 2008] 

...