What are the valid signatures for C's main() function?
This question already has an answer here:
The current standard as at the time of this answer (C11) explicitly mentions these two:
int main(void);
int main(int argc, char* argv[]);
although it does mention the phrase "or equivalent" with the following footnote:
Thus, int
can be replaced by a typedef
name defined as int
, or the type of argv
can be written as char ** argv
, and so on.
In addition, it also provides for more (implementation-defined) possibilities.
The relevant section (section 5.1.2.2.1 in C11, but this particular aspect is unchanged from C99) states:
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.
If they are declared, the parameters to the main function shall obey the following constraints:
The value of argc
shall be nonnegative.
argv[argc]
shall be a null pointer.
If the value of argc is greater than zero, the array members argv[0]
through argv[argc-1]
inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. 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[0]
represents the program name; argv[0][0]
shall be the null character if the program name is not available from the host environment. If the value of argc
is greater than one, the strings pointed to by argv[1]
through argv[argc-1]
represent the program parameters.
The parameters argc
and argv
and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
Note that this is for a hosted environment, the ones you normally see in C programs. A free-standing environment (such as an embedded system) is far less constrained, as stated in 5.1.2.1 of that same standard:
In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.
POSIX supports execve()
, which in turn supports
int main(int argc, char *argv[], char *envp[])
The added argument is the environment, ie an array of strings of the form NAME=VALUE.
Standard C
For a hosted environment (that's the normal one), the C99 standard says:
5.1.2.2.1 Program startup
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;9) or in some other implementation-defined manner.
9) Thus, int
can be replaced by a typedef name defined as int
, or the type of argv
can be written as char **argv
, and so on.
Standard C++
The C++98 standard says:
3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. [...]
2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
The C++ standard explicitly says "It [the main function] shall have a return type of type int, but otherwise its type is implementation defined", and requires the same two signatures as the C standard. So a 'void main()' is directly not allowed by the C++ standard, though there's nothing it can do to stop a non-standard implementation allowing alternatives.
Common Extension
Classically, Unix systems support a third variant:
int main(int argc, char **argv, char **envp) { ... }
The third argument is a null-terminated list of pointers to strings, each of which is an environment variable which has a name, an equals sign, and a value (possibly empty). If you do not use this, you can still get at the environment via ' extern char **environ;
'. For a long time, that did not have a header that declared it, but the POSIX 2008 standard now requires it to be declared in <unistd.h>
.
This is recognized by the C standard as a common extension, documented in Annex J:
J.5.1 Environment arguments
¶1 In a hosted environment, the main function receives a third argument, char *envp[]
, that points to a null-terminated array of pointers to char
, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).
Microsoft C
The Microsoft VS 2010 compiler is interesting. The web site says:
The declaration syntax for main is
int main();
or, optionally,
int main(int argc, char *argv[], char *envp[]);
Alternatively, the main
and wmain
functions can be declared as returning void
(no return value). If you declare main
or wmain
as returning void, you cannot return an exit code to the parent process or operating system by using a return statement. To return an exit code when main
or wmain
is declared as void
, you must use the exit
function.
It is not clear to me what happens (what exit code is returned to the parent or o/s) when a program with void main()
does exit — and the MS web site is silent too.
Interestingly, MS does not prescribe the two-argument version of main()
that the C and C++ standards require. It only prescribes a three argument form where the third argument is char **envp
, a pointer to a list of environment variables.
The Microsoft page also lists some other alternatives — wmain()
which takes wide character strings, and some more.
The Microsoft VS 2005 version of this page does not list void main()
as an alternative. The versions from Microsoft VS 2008 onwards do.
上一篇: main()函数的返回类型
下一篇: C的main()函数的有效签名是什么?