BGB said:
this claim is mostly for C, but matters get a little more complex if C++
is involved (AFAIK, the "(void)" syntax is not officially supported in C++).
"()" works as expected in both languages (despite the slightly different
semantics between C and C++, as it is most commonly used they are
functionally equivalent), and IMO people demanding the use of "(void)"
are making a fuss over nothing.
or such...
In C, empty parentheses in a function declaration:
int foo()
mean that the function takes an unspecified number and type(s) of
argument(s). If the function takes no arguments, it's better to
say so explicitly:
int foo(void)
If empty parentheses appear in a declaration that's part of a
definition:
int foo()
{
/* ... */
}
then the function has no parameters -- but the compiler still isn't
required to check that any calls pass no arguments, and it's still
better to be explicit.
In the special case of main, calls are not usually an issue, but the
standard requires
int main(void)
or
int main(int argc, char *argv[])
"or equivalent; or in some other implementation-defined manner."
Here's where it gets a little tricky. The above is worded as a
"shall" requirement outside a constraint (see C99 5.1.2.2.1),
which means that violating it causes the program's behavior
to be undefined.
For the reasons I've mentioned above, I argue that "int main()" is
not *equivalent* to "int main(void)", so unless the implementation
documents that it accepts "int main()", the behavior of such a
program is undefined. In every C implementation I've seen (not that
many), a program with "int main()" works the same way as a program
with "int main(void)" (largely, I suppose, for historical reasons,
given that pre-ANSI C didn't have the "void" keyword). This is,
of course, one possible result of undefined behavior.
If you care about conforming to the C standard, write "int main(void)".
If you don't care quite as much, you can almost certainly get away
with "int main()", but that's not the way I'd go.
<OT>
In C++, "int main()" is the preferred form. "int main(void)" is
also accepted for C compatibility. If you care about making your
C++ code compilable as C, you should think carefully about why you
care about it. If you *still* care about it, you can safely use
"int main(void)" in either language, with no risk of misbehavior
on any conforming implementation. If you just want to write C++,
use "int main()", and give your source file a suffix that will
prevent any pure C compiler from accepting it.
</OT>