Related
What really are the valid signatures for main function in C? I know:
int main(int argc, char *argv[])
Are there other valid ones?
The C11 standard 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 text (section 5.1.2.2.1, 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.
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.
The C11 and C18 standards say essentially the same as the C99 standard.
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 conforming implementation from allowing alternatives (nor a standard conforming implementation from allowing alternatives as extensions to the standard).
The C++03, C++11, C++14, and C++17 standards say essentially the same as C++98.
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;'. This variable is (still) not declared in any POSIX header (previous versions of this answer notwithstanding).
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.
Is int main() the same as int main(void)?
For a detailed analysis, see the end of my answer to What should main() return in C and C++. (It seems that I once considered that this question referred to C++, even though it doesn't and never did. In C++, there is no difference between int main() and int main(void) and int main() is idiomatic C++.)
In C, there is a difference between the two notations, but you only notice it in esoteric cases. Specifically, there's a difference if you call the main() function from your own code, which you're allowed to do in C and are not allowed to do in C++.
The int main() notation does not provide a prototype for main(), but that only matters if you call it recursively. With int main(), you might later (in the same function, or in another function) write int rc = main("absolute", "twaddle", 2): and formally the compiler shouldn't complain to the extent of refusing to compile the code, though it might legitimately complain (warn you) about it (and using -Werror with GCC would convert the warning into an error). If you use int main(void), the subsequent call to main() should generate an error — you said the function takes no arguments but tried to provide three. Of course, you can't legitimately call main() before you've declared or defined it (unless you are still using C90 semantics) — and the implementation does not declare a prototype for main(). NB: The C11 standard illustrates both int main() and int main(void) in different examples — both are valid in C, even though there's the subtle difference between them.
POSIX supports execve(), which in turn supports
int main(int argc, char *argv[], char *envp[])
The added argument is the environment, i.e. an array of strings of the form NAME=VALUE.
http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B
Besides the usual int main(int argc, char *argv[]) and the POSIX int main(int argc, char **argv, char **envp), on Mac OS X also supports
int main(int argc, char* argv[], char* envp[], char* apple[]);
Of course it's Mac-only.
On Windows there's
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
as the Unicode (actually, wide-character) variant. Of course there is WinMain too.
int main(void)
Under some OS (for example, Windows) also such is valid:
int main(int argc, char **argv, char **envp)
where envp gives an environment, otherwise accessible through getenv()
What really are the valid signatures for main function in C? I know:
int main(int argc, char *argv[])
Are there other valid ones?
The C11 standard 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 text (section 5.1.2.2.1, 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.
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.
The C11 and C18 standards say essentially the same as the C99 standard.
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 conforming implementation from allowing alternatives (nor a standard conforming implementation from allowing alternatives as extensions to the standard).
The C++03, C++11, C++14, and C++17 standards say essentially the same as C++98.
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;'. This variable is (still) not declared in any POSIX header (previous versions of this answer notwithstanding).
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.
Is int main() the same as int main(void)?
For a detailed analysis, see the end of my answer to What should main() return in C and C++. (It seems that I once considered that this question referred to C++, even though it doesn't and never did. In C++, there is no difference between int main() and int main(void) and int main() is idiomatic C++.)
In C, there is a difference between the two notations, but you only notice it in esoteric cases. Specifically, there's a difference if you call the main() function from your own code, which you're allowed to do in C and are not allowed to do in C++.
The int main() notation does not provide a prototype for main(), but that only matters if you call it recursively. With int main(), you might later (in the same function, or in another function) write int rc = main("absolute", "twaddle", 2): and formally the compiler shouldn't complain to the extent of refusing to compile the code, though it might legitimately complain (warn you) about it (and using -Werror with GCC would convert the warning into an error). If you use int main(void), the subsequent call to main() should generate an error — you said the function takes no arguments but tried to provide three. Of course, you can't legitimately call main() before you've declared or defined it (unless you are still using C90 semantics) — and the implementation does not declare a prototype for main(). NB: The C11 standard illustrates both int main() and int main(void) in different examples — both are valid in C, even though there's the subtle difference between them.
POSIX supports execve(), which in turn supports
int main(int argc, char *argv[], char *envp[])
The added argument is the environment, i.e. an array of strings of the form NAME=VALUE.
http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B
Besides the usual int main(int argc, char *argv[]) and the POSIX int main(int argc, char **argv, char **envp), on Mac OS X also supports
int main(int argc, char* argv[], char* envp[], char* apple[]);
Of course it's Mac-only.
On Windows there's
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
as the Unicode (actually, wide-character) variant. Of course there is WinMain too.
int main(void)
Under some OS (for example, Windows) also such is valid:
int main(int argc, char **argv, char **envp)
where envp gives an environment, otherwise accessible through getenv()
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 8 years ago.
I have a couple of questions regarding § 5.1.2.2.2 in the standard.
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;10) or in some other
implementation-defined manner.
What does "some other implementation-defined manner" refer to? Does it refer to footnote 10, which states:
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.
...or the actual definition of main? If so, does this mean that given a certain implementation, the compiler can say main should be defined however it wants (this runs contrary to what I've read on the internet saying it must be int main. For example, in § 5.1.2.2.3 it states:
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent
to calling the exit function with the value returned by the main
function as its argument;11) reaching the } that terminates the main
function returns a value of 0. If the return type is not compatible
with int, the termination status returned to the host environment is
unspecified.
...which seems to leave a lot of leeway to the implementation.
— 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.
What does "retain their last-stored values mean"? Does this mean functions like getopt violate the standard because they permute argv?
What does "some other implementation-defined manner" refer to?
The C standard provides only two signatures for main:
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
But some implementations also allow some different signatures.
On Mac OS:
int main(int argc, char* argv[], char* envp[], char* apple[]);
On Windows:
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
POSIX supports
int main(int argc, char *argv[], char *envp[])
No; it means that Microsoft can document:
void main(int argc, char **argv, char **envp)
as a valid signature for main() on their system and it is valid as a result of the 'or in some other implementation-defined manner'.
The footnote means you can use:
typedef int num;
num main(num argc, char **argv)
and it remains valid.
See also What should main() return in C and C++.
The 'last modified values' bit means that when getopt() permutes the values in argv, the permutations it sets stay in effect until overridden by something else changing the values in argv.
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1;i<argc;i++)
printf("%s%s", argv[i], (i<argc-1)? " ":"");
printf("\n");
return 0;
}
Given above is a simple C program that outputs command line inputs. Here argc is the argument counter. argv is said to be an array that contains arguments. My question is: why does it define as a pointer to a character array instead of a normal array? Also what is the need for defining its zeroth element (argv[0]) as the name by which the program is invoked.
I am a beginner and please explain it high level perspective.
argv is defined as a pointer rather than as an array because there is no such thing as an array parameter in C.
You can define something that looks like an array parameter, but it's "adjusted" to array type at compile time; for example, these two declarations are exactly equivalent:
int foo(int param[]);
int foo(int param[42]); /* the 42 is quietly ignored */
int foo(int *param); /* this is what the above two declarations really mean */
And the definition of main can be written either as:
int main(int argc, char *argv[]) { /* ... */ }
or as
int main(int argc, char **argv) { /* ... */ }
The two are exactly equivalent (and the second one, IMHO, more clearly expresses what's actually going on).
Array types are, in a sense, second-class types in C. Code that manipulates array almost always does so via pointers to the elements, performing pointer arithmetic to traverse the elements.
Section 6 of the comp.lang.c FAQ explains the often confusing relationship between arrays and pointers.
(And if you've been told that arrays are "really" pointers, they're not; arrays and pointers are distinct things.)
As for why argv[0] points to the program name, that's just because it's useful. Some programs print their names in error messages; others may change their behavior depending on the name by which they're invoked. Bundling the program name with the command-line arguments was a fairly arbitrary choice, but it's convenient and it works.
The char *argv[] is a pointer that an array of char * has decayed into. For example, invoking a command like this:
$ ./command --option1 -opt2 input_file
could be viewed as:
char *argv[] = {
"./command",
"--option1",
"-opt2",
"input_file",
NULL,
};
main(4, argv);
So basically there is an array of strings outside main, and it is passed to you in main:
char *argv[]
\- --/ ^
V |
| It was an array
|
of strings
Regarding argv[0] being the invocation command, the reason is largely historical. I don't know what the first person who thought of it thought about, but I can tell at least one usefulness for it.
Imagine a program, such as vim or gawk. These programs may install symbolic links (such as vi or awk) which point to the same program. So effectively, running vim or vi (or similarly gawk or awk) could execute the exact same program. However, by inspecting argv[0], these programs can tell how they have been called and possibly adjust accordingly.
As far as I know, neither of the programs I mentioned above actually do this, but they could. For example vim called through a symbolic link named vi could turn on some compatibility. Or gawk called as awk could turn off some GNU extensions. In the modern world, if they wanted to do this, they would probably create scripts that gives the correct options, though.
The questions you ask are really answered best by simply saying its all "by definition". i.e. a set of rules designed and agreed upon by a committee.
Here is what C11 says: (see emphasized sections)
5.1.2.2.1 Program startup
1 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;10) or in some other implementation-defined manner.
2 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.
It is not defined as a normal array because in C the size of array elements has to be known at compile time. The size of char * is known, the size (length) of your arguments are not.
argv[0] contains the name of the invoked process because it is possible to invoke it by any arbitrary name. e.g. exec family of calls can specify what it wants and you are allowed to invoke a program via a symlink. argv[0] allows the program to offer different functionality depending on the invocation name.
What really are the valid signatures for main function in C? I know:
int main(int argc, char *argv[])
Are there other valid ones?
The C11 standard 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 text (section 5.1.2.2.1, 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.
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.
The C11 and C18 standards say essentially the same as the C99 standard.
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 conforming implementation from allowing alternatives (nor a standard conforming implementation from allowing alternatives as extensions to the standard).
The C++03, C++11, C++14, and C++17 standards say essentially the same as C++98.
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;'. This variable is (still) not declared in any POSIX header (previous versions of this answer notwithstanding).
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.
Is int main() the same as int main(void)?
For a detailed analysis, see the end of my answer to What should main() return in C and C++. (It seems that I once considered that this question referred to C++, even though it doesn't and never did. In C++, there is no difference between int main() and int main(void) and int main() is idiomatic C++.)
In C, there is a difference between the two notations, but you only notice it in esoteric cases. Specifically, there's a difference if you call the main() function from your own code, which you're allowed to do in C and are not allowed to do in C++.
The int main() notation does not provide a prototype for main(), but that only matters if you call it recursively. With int main(), you might later (in the same function, or in another function) write int rc = main("absolute", "twaddle", 2): and formally the compiler shouldn't complain to the extent of refusing to compile the code, though it might legitimately complain (warn you) about it (and using -Werror with GCC would convert the warning into an error). If you use int main(void), the subsequent call to main() should generate an error — you said the function takes no arguments but tried to provide three. Of course, you can't legitimately call main() before you've declared or defined it (unless you are still using C90 semantics) — and the implementation does not declare a prototype for main(). NB: The C11 standard illustrates both int main() and int main(void) in different examples — both are valid in C, even though there's the subtle difference between them.
POSIX supports execve(), which in turn supports
int main(int argc, char *argv[], char *envp[])
The added argument is the environment, i.e. an array of strings of the form NAME=VALUE.
http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B
Besides the usual int main(int argc, char *argv[]) and the POSIX int main(int argc, char **argv, char **envp), on Mac OS X also supports
int main(int argc, char* argv[], char* envp[], char* apple[]);
Of course it's Mac-only.
On Windows there's
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);
as the Unicode (actually, wide-character) variant. Of course there is WinMain too.
int main(void)
Under some OS (for example, Windows) also such is valid:
int main(int argc, char **argv, char **envp)
where envp gives an environment, otherwise accessible through getenv()