In C/C++ language, main function comes in different flavors as shown below:
int main(); int main(int argc, char **argv); int main(int argc, char **argv, char **envp); int main(int argc, char **argv, char **envp, char **apple);
What is the signature of main specified in the language standard ?
You may want to refer to page-12 in the C99 standard of C language, or just read below:
The return type of main function should be an int and it be defined in with either no parameters, as below (any of the two declarations):
int main(); int main(void);
or 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) as shown below (both declarations are equivalent):
int main(int argc, char *argv); int main(int argc, char **argv);
The two parameters to main function should follow the below rules:
- The value of argc shall be non-negative.
- argv[argc] shall be a NULL Pointer.
- If the value of argc is greater than zero, the array members argv through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup (command-line parameters). The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
- If the value of argc is greater than zero, the string pointed to by argv represents the program name;
- If program name is not available from the host environment then argv shall be the null character. If the value of argc is greater than one, the strings pointed to by argv through argv[argc-1] represent the program parameters (Command-line parameters).
- The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program (Hence not const char*), and retain their last-stored values between program startup and program termination.
Other platform-dependent signatures of main function are also allowed by the C and C++ standards, except that in C++ the return type must always be int.
Signature of main on Windows & Unix:
Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program’s environment (the environment variables), otherwise accessible through getenv in stdlib.h:
int main(int argc, char **argv, char **envp);
Signature of main on MAC OS X
Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:
int main(int argc, char **argv, char **envp, char **apple);
The value returned from main function becomes the exit status of the program.
The C standard only ascribes specific meaning to two values: EXIT_SUCCESS (traditionally 0) and EXIT_FAILURE (non-zero).