Because many programmers used the second style, causing unspecified exit status to be reported to the system, the C Standard committee decided to make main
return 0
implicitly if control leaves its body without a return
statement. This behavior is mandated by the C Standard C99 and later versions. As a consequence, return 0;
can be omitted by it is better IMHO to still make it explicit.
Note however that it is also considered good style to indent the statements in the body of functions:
int main() {
stuff;
return 0;
}
Note also that the C Standard documents 2 possible prototypes for main
:
int main(void);
and
int main(int argc, char *argv[]);
or equivalent variants:
int main(int argc, char **argv[]);
int main(const int argc, char ** const argv);
etc.
Omitting the argument list as you wrote in both examples is supported and would be equivalent to int main(void)
in C++, but is not exactly equivalent in C: It means the argument list is unspecified, so the compiler cannot check the arguments passed to main
if it encounters a call to main
in the program, not can it perform the appropriate conversions.
In this case, it does not matter since the main
functions in the examples do not use their arguments and indeed seems more consistent than int main(void)
since arguments are indeed passed to it by the startup code.