The C standard say:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent or in some other implementation-defined manner.
However, Kernighan & Ritchie in their second edition (the canonical ANSI C) bible just use:
main()
{
/* taram pampam ... */
return 0;
}
Who is right?
Does it have to do with function without return value automatic assume to be returning int
in C?
Well, if you want ANSI C, then by definition the standard is right.
In C89/C90 the
int
return type is implied, so the K&R definition would be acceptable.In C99 this is no longer the case.
The C90 standard has the following wording (5.1.2.2.1 Program startup), which is very similar to the C99 wording (probably most significantly it uses the less strong 'can' instead of 'shall'):
There's nothing in that section directly about the fact that leaving off the return type will result in it defaulting to
int
.Frankly, I have a hard time finding exactly where that behavior is specified by the standard. The closest I can come is in 6.7.1 (Functions definitions) where the grammar for function definitions indicates that the 'declaration-specifiers' are optional, and the examples say: