tags:

views:

457

answers:

4

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?

+8  A: 

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'):

The function called at program startup is named main. The implementation declares no prototype for this function. It can be defined 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[]) { /* ... */ }

If they are defined, the parameters to the main function shall obey the following constraints:

[etc. ...]

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:

Examples:

  1. In the following:

      extern int max(int a, int b)
      {
          return a > b ? a : b;
      }
    

    extern is the storage class specifier and int is the type specifier (each of which may be omitted as those are the defaults)...

Michael Burr
oh, didn't know about the difference between 99 and 89 here, perhaps this is the reason because I quoting from 99! Do you have the 89/90 standard and can quote the parallel paragraph about main from it?
zaharpopov
The C89 standard said much the same; but in C89, a function without an explicit return type was implicitly assumed to return int, so the int-less form was equivalent.
Jonathan Leffler
@zaharpopov - I added details from the older standard.
Michael Burr
+3  A: 

Yes, in C89 (the original C standard), if a function is declared without a return type, it is assumed to return int. C99 requires an explicit return type on all functions.

Eric Petroelje
but doesn't their definition violate the standard in some way, anyhow?
zaharpopov
No. If you imply an int there, then it matches the acceptable definitions of main.
GMan
Zaharpopov: Their definition was legal (and the authority) at the time. C99 (which came later) now forbids implicit int return types.
Roger Pate
+3  A: 

Also, there's a subtle difference (at least in declarations) between main() and main(void) --

main()

is a function (implicitly) returning int and taking an unspecified number of arguments

main(void)

takes no arguments.

Steven Schlansker
That is only in a declaration - and I don't know of anyone who declares main(). In a definition, the two are equivalent, though the explicit 'int main(void)' notation is generally the better C style; in C++, omitting the 'void' is perfectly acceptable.
Jonathan Leffler
I've modified my post to reflect that. While it may not be the case in main specifically, it's worth noting this for other functions...
Steven Schlansker
A: 

The version of K&R I have was printed in 1988. The Standard wasn't out by then, so there are some inconsistencies. However, most of the 2nd edition complies with the C89 Standard.

I found a text version of the C89 Standard (YAY for Google); it says:

"Program startup"

The function called at program startup is named main . The implementation declares no prototype for this function. It can be defined 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[]) { /*...*/ }
pmg
zaharpopov