According to section 6.7.5.3 Function declarators (including prototypes), paragraph 14, of C99:
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.124)
124) See "future language directions" (6.11.6).
Section 6.11.6 Function declarators, states that:
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
Consequently, functions Functions that accept no arguments should explicitly declare a void
parameter in their parameter list. This holds true in both the declaration and definition sections (which should match). Many compilers today still allow implicitly declared functions, even though C99 has eliminated them.
Wiki Markup |
---|
Defining a function with a {{void}} argument list differs from declaring it with no arguments because in the latter case, the compiler will not check whether the function is called with parameters at all \[[C void usage|http://tigcc.ticalc.org/doc/keywords.html#void]\]. Consequently, function calling with arbitrary parameters will be accepted without a warning at compile time. |
Failure to declare a void
parameter will result in
- an ambiguous functional interface between the caller and callee
- sensitive information outflow
...
Because no function parameter has the same meaning as an arbitrary parameter, the caller can provide an arbitrary number of arguments to the function.
Code Block | ||
---|---|---|
| ||
/* in foo.h */ void foo(); /* in foo.c */ void foo() { int i = 3; printf("i value: %d\n", i); } ... /* in caller.c */ #include "foo.h" foo(3); |
Compliant Solution (Ambiguous Interface)
In this compliant solution, void
is specified explicitly as a parameter in the declaration of foo
's prototype.
Code Block | ||
---|---|---|
| ||
/* in foo.h * compile using gcc4.3.3/ void foo(void); /* in foo.c */ void foo(void) { int i = 3; printf("i value: %d\n", i); } /* in caller.c */ #include "foo.h" foo(3); |
Implementation Details (Ambiguous Interface)
...