3

Possible Duplicate:
What are the valid signatures for C's main() function?

What are the different valid prototypes of 'main' function?

Are there some non-standard prototypes also supported only by a few vendors?

Community
  • 1
  • 1
Jay
  • 22,129
  • 23
  • 82
  • 131

2 Answers2

4

The C standard (§ 5.1.2.2.1) defines two entry point prototypes:

int main(void);

or

int main(int argc, char **argv);

Other than that, every OS has its own additional non-standard entry points. WinMain, etc.

Alex Budovski
  • 16,568
  • 6
  • 48
  • 57
  • You haven't quite covered it all. Note also that an alternative to `int main(int argc, char **argv);` is `int main(int argc, char * argv[]);`. In both cases you are saying `argv` is an array of elements of type `char *`, or otherwise stated: an array of C-strings. – Gabriel Staples Oct 17 '19 at 19:36
  • Here's some more good documentation: https://en.cppreference.com/w/c/language/main_function. – Gabriel Staples Oct 17 '19 at 19:45
2

The full prototype allowed by gcc is:

int main(int argc, char * argv[], char *envp[])

but envp is rarely used. Omitting argc and argv is also considered acceptable.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • 1
    `envp` is not specified in C. C includes the two listed by Alex, but it allows "some other implementation-defined manner", including that one. – Matthew Flaschen Nov 25 '10 at 03:55
  • @Matthew: Interestingly, `gcc -pedantic` does not complain about `envp`. – Ignacio Vazquez-Abrams Nov 25 '10 at 03:56
  • 1
    Of course not. The standard _allows_ implementation-defined additional prototypes, so as long as GCC documents the extra parameter(s), it's standards-conformant. – Chris Lutz Nov 25 '10 at 03:59
  • true. I checked, and the specific warning (enabled by -pedantic) is -Wmain. That says "main should be a function with external linkage, returning int, taking either zero arguments, two, or three arguments of appropriate types." So it's being a little lax, but that's okay, because of the phrase I mentioned. :) – Matthew Flaschen Nov 25 '10 at 04:00