Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed implicit prototype examples to utilize two files

Function declarators must be declared with the appropriate type information, including a return type , and 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.

...

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 Declaring a function without any prototype forces the compiler to assume that the correct number and type of parameters have been supplied to a function. This can result in unintended and undefined behavior.

In this non-compliant code example, which involves two files, the definition of func() expects three parameters but is supplied only two. However, because there is no prototype for func() in the second file, 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
int func(int one, int two, int three){
  printf("%d %d %d", one, two, three);
  return 1;
}
/* ... */
Code Block
bgColor#FFCCCC
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 still allow compilation of programs containing implicitly defineddeclared functions, although they may issue a warning message. These warnings should be resolved (see [MSC00-A. Compile cleanly at high warning levels]).

...

To correct this example, the appropriate function prototype for func() should be specified in the file in which it is invoked. This will allow a compiler to recognize invalid function arguments:

Code Block
bgColor#ccccff
int func(int, int, int);

func(1, 2);

The user can then correct the code:

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

int func(int, int, int);

func(1, 2, 3);

Non-Compliant Code Example (Function Pointers)

...