Failure to specify function prototypes results in a function being implicitly defined. Without a function prototype, the compiler will assume the the correct number and type of parameters have been supplied to a function. Calling a function with a different number of arguments then that function expects results in undefined, and perhaps unintended behavior.
Wiki Markup |
---|
C99 removed implicit function declarations from the C language [\[ref]\]. However, compilers will typically allow compilation of programs that contain implicitly defined functions, although they will issue a warning. These warnings should be resolved \[[MSC00-A|MSC00-A. Compile cleanly at high warning levels]\], but they will not prevent program compilation []. Given this, functions should be declared with the appropriate function prototype. |
Non-Compliant Code Example 1
In this 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 parameters has been supplied, using the next value on the program stack as the missing third argument.
Code Block |
---|
|
function(1, 2);
...
int func(int one, int two, int three){
printf("%d %d %d", one, two, three);
return 1;
}
|
...
Code Block |
---|
|
int function(int,int,int);
...
function(1,2);
...
int func(int one, int two, int three){
printf("%d %d %d", one, two, three);
return 1;
}
|
...