The forms that omit the return type are valid only in versions of C prior to the 1999 standard. In older versions of C, omitting the return type was equivalent to declaring an explicit return type of int
. In C99 and later, omitting the return type is not permitted.
The forms that declare the return type as void
are non-standard, and often a sign of a textbook written by someone who doesn't know the language very well. The only standard return type for main
is int
.
The forms that use empty parentheses ()
rather than (void)
are obsolescent. The versions with the empty parentheses are old-style non-prototype definitions. It's still legal to use old-style definitions for functions in general; it's debatable whether it's permitted for main
in particular. There's no good reason to use ()
rather than (void)
.
The permitted forms are:
int main(void) { /* ... */ }
and
int main(int argc, char *argv[]) { /* ... */ }
or equivalent. The "or equivalent" means that you can use an equivalent typedef in place of int
or char
(but please don't), that you can spell the parameter names differently (but please don't), and that you can use char **argv
rather than char *argv[]
(char **argv
is actually a more direct representation of the real parameter type).
The standard follows these two forms with the phrase "or in some other implementation-defined manner". Implementations may, but are not required to, support other forms, such as:
int main(int argc, char *argv[], char *envp[]) { /* ... */ } /* NOT PORTABLE */
or even
void main(void) { /* ... */ } /* NOT PORTABLE */
Using such an alternate form is rarely necessary; sticking strictly to one of the two standard forms is good enough 99% of the time and makes your code more portable.
Of the forms in your question, only the last:
int main(void){return 0;}
is definitively valid. The third:
int main(){return 0;}
is probably valid, but you might as well add the void
keyword and remove all doubt.
(An irrelevant aside: int main() { /* ... */ }
is valid in C++, where the empty parentheses have a different meaning.)
(C uses (void)
to indicate that a function has no parameters because the ()
syntax is already used for another purposes, to indicate that a function has an unspecified number and type of parameters. That usage of empty parentheses is obsolescent, and should be avoided in new code.)