Versions Compared

Key

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

Function declarators must be declared with the appropriate type information, including a return type, parameter list, and function prototype (if the declarator is part of a function definition). If type information is not properly specified in a function declarator, the compiler cannot properly check function type information. When using standard library calls, the easiest (and preferred) way to obtain function declarators with appropriate type information is to include the appropriate header file.

Wiki Markup
Attempting to compile a program with a function declarator that does not include the appropriate type information typically generates a warning. These warnings should be resolved \[[MSC00-A. Compile cleanly at high warning levels]\], but do not prevent program compilation.

Non-Compliant Code Example (malloc())

Wiki Markup
The following example is based on rule \[[MEM02 These warnings should be resolved \[[MSC00-A. DoCompile notcleanly castat thehigh return value from malloc()]\]. The header file {{stdlib.h}} contains the function prototype for {{malloc()}}. Failing to include {{stdlib.h}} causes {{malloc()}} to be improperly defined.

Code Block
bgColor#FFCCCC

char *p = malloc(10);

Compliant Solution: (malloc())

Including stdlib.h ensures the function prototype for malloc() is declared and in scope.

Code Block
bgColor#ccccff

#include <stdlib.h>
/* ... */
char *p = malloc(10);

warning levels]\].

Non-Compliant Code Example: (non-prototype-format declarators)

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

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

Section 6.11 of the C99 standardsstandard, "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."

...

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;
}

...

In this non-compliant code example, the definition of func() expects three parameters but is supplied only two. However, because there is no prototype for func(), the compiler assumes that the correct number of arguments has been supplied, and uses the next value on the program stack as the missing third argument.

Code Block
bgColor#FFCCCC

func(1, 2);
/* ... */
int func(int one, int two, int three){
  printf("%d %d %d", one, two, three);
  return 1;
}
/* ... */
func(1, 2);

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 willstill allow compilation of programs containing implicitly defined functions, although they may issue a warning message. These warnings should be resolved \[[MSC00-A. Compile cleanly at high warning levels]\], but do not prevent program compilation.

Compliant Solution: (function prototypes)

...

Code Block
bgColor#ccccff
int func(int, int, int);
/* ... */

func(1, 2, 3);
/* ... */
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.

Wiki Markup
If aIn this non-compliant code example, the function pointer is set{{fn_ptr}} refers to refer to an incompatiblethe function {{add()}}, invokingwhich thataccepts functionthree viainteger thearguments. pointer may cause unexpected dataHowever, {{fn_ptr}} is specified to beaccept takentwo from the process stackinteger arguments. As a result, unexpected data may be accessed by the called function. Setting {{fn_ptr}} to refer to {{add()}} results in an unexpected program behavior.  This example also violates rule \[[DCL35-C. Do not convert a function pointer to a function of an incompatible type]\].
In this example, the function pointer fn_ptr is set to refer to the function add(), which accepts three integer arguments. However, fn_ptr is specified to accept two integer arguments. Setting fn_ptr to refer to add() results in an unexpected program behavior
.

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;
}

...