The word "void" is required in a function prototype to indicate the function has no parameters. However, an empty list in a function declarator which is part of a function declaration specifies that the function has no parameters.
Now I'm confused. I thought that in C a function declared with an empty parameter list is an unprototyped function taking an unspecified number of arguments.
Style standards might suggest that "void" be used in both cases, but the language standard doesn't require it.
In C a function declared with a parameter list consisting of only void
is a prototyped function taking no arguments.
See
http://david.tribble.com/text/cdiffs.htmwhich in part states,
Empty parameter lists
C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of only void. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.
// C code
extern int foo(); // Unspecified parameters
extern int bar(void); // No parameters
void baz()
{
foo(0); // Valid C, invalid C++
foo(1, 2); // Valid C, invalid C++
bar(); // Okay in both C and C++
bar(1); // Error in both C and C++
}
C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.
// C++ code
extern int xyz();
extern int xyz(void); // Same as 'xyz()' in C++,
// Different and invalid in C
For code that is intended to be compiled as either C or C++, the best solution to this problem is to always declare functions taking no parameters with an explicit void prototype. For example:
// Compiles as both C and C++
int bosho(void)
{
...
}
Empty function prototypes are a deprecated feature in C99 (as they were in C89).
[C99: §6.7.5.3]
[C++98: §8.3.5, C.1.6.8.3.5]
Robert Wishlaw