Page 83 - C-Language
P. 83
another example).
C11 section 6.7.6.3 "Function declarators", paragraph 10, states:
The special case of an unnamed parameter of type void as the only item in the list
specifies that the function has no parameters.
Paragraph 14 of that same section shows the only difference:
… 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.
A simplified explanation provided by K&R (pgs- 72-73) for the above stuff:
Furthermore, if a function declaration does not include arguments, as in
double atof();, that too is taken to mean that nothing is to be assumed about the
arguments of atof; all parameter checking is turned off. This special meaning of the
empty argument list is intended to permit older C programs to compile with new
compilers. But it's a bad idea to use it with new programs. If the function takes
arguments, declare them; if it takes no arguments, use void.
So this is how your function prototype should look:
int foo(void);
And this is how the function definition should be:
int foo(void)
{
...
<statements>
...
return 1;
}
One advantage of using the above, over int foo() type of declaration (ie. without using the
keyword void), is that the compiler can detect the error if you call your function using an erroneous
statement like foo(42). This kind of a function call statement would not cause any errors if you
leave the parameter list blank. The error would pass silently, undetected and the code would still
execute.
This also means that you should define the main() function like this:
int main(void)
{
...
<statements>
...
return 0;
https://riptutorial.com/ 59

