The two canonical forms of main
are, according to the standard (see C99 section 5.1.2.2.2 "Program startup"):
int main (void);
int main (int argc, char *argv[]); // or equivalent such as char **argv
Others are specifically allowed but those are the required ones.
As to the preferred form between fn(void)
and fn()
, I prefer the former since I like to explicitly state that there are no parameters.
There is also a subtle difference. C99 section 6.7.5.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 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.
In other words, it means the same as void
in the function definition but does not mean that in a standalone declarator (i.e., the prototype). int fn();
means that no information on the parameters is yet known but int fn(void);
means there are no parameters.
That means that:
int fn();
int fn (int x) { return x; }
int main (void) { return fn(0); }
is valid but:
int fn(void);
int fn (int x) { return x; }
int main (void) { return fn(0); }
is not.