Versions Compared

Key

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

...

Attempting to compile a program with a function declarator that does not include the appropriate type information typically generates a warning but does not prevent program compilation. These warnings should be resolved (see MSC00-A. Compile cleanly at high warning levels).

Non-Compliant Code Example (

...

Non-

...

Prototype-

...

Format Declarators)

The non-compliant code example uses the identifier-list form for parameter declarations.

...

Section 6.11 of the C99 standard, "Future language directions," states that "The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature."

Compliant Solution (

...

Non-

...

Prototype-

...

Format Declarators)

In this compliant solution, extern is the storage-class specifier and int is the type specifier; max(int a, int b) is the function declarator; and the block within the curly braces is the function body.

Code Block
bgColor#ccccff
extern int max(int a, int b) {
  return a > b ? a : b;
}

Non-Compliant Code Example (

...

Function Prototypes)

Failure to specify function prototypes results in a function being implicitly defined. Without a function prototype, the compiler assumes that the correct number and type of parameters have been supplied to a function. This can result in unintended and undefined behavior.

...

Wiki Markup
C99 eliminated implicit function declarations from the C language \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]. However, many compilers still allow compilation of programs containing implicitly defined functions, although they may issue a warning message. These warnings should be resolved (see [MSC00-A. Compile cleanly at high warning levels]).

Compliant Solution (

...

Function Prototypes)

To correct this example, the appropriate function prototype for func() should be specified.

Code Block
bgColor#ccccff
int func(int, int, int);
/* ... */
int func(int one, int two, int three){
  printf("%d %d %d", one, two, three);
  return 1;
}
/* ... */
func(1, 2, 3);

Non-Compliant Code Example (

...

Function Pointers)

If a function pointer refers to an incompatible function, invoking that function via the pointer may corrupt the process stack. As a result, unexpected data may be accessed by the called function.

...

Code Block
bgColor#FFCCCC
int add(int x, int y, int z) {
   return x + y + z;
}

int main(int argc, char *argv[]) {
   int (*fn_ptr) (int, int);
   int res;
   fn_ptr = add;
   res = fn_ptr(2, 3);  /* incorrect */
   /* ... */
   return 0;
}

Compliant Solution (

...

Function Pointers)

To correct this example, the declaration of fn_ptr is changed to accept three arguments.

...