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 are the valid signatures for C's main() function?
(5 answers)
Closed 3 years ago.
What is the difference between main, void main and int main in C? what does each one of them do?
Also what is return 0; used for? i know it somehow tells the OS that the program finished succesfully but what does that have to offer?
I would like to note that ive been working on C for a little more than a month so im not really experienced
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.
10) 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.
C 2011 Online Draft
main() is equivalent to int main(void). Under earlier versions of the language, if you defined a function without an explicit return type, the compiler assumed it returned int. Also, if you define a function without any parameters, that means the function takes no parameters. Implicit typing is no longer allowed, and using prototype syntax allows you to catch errors in the number and types of arguments at compile time, so this form should no longer be used.
void main() is not standard, and should not be used unless your implementation explicitly documents it as a valid signature for main ("or in some other implementation-defined manner")1. Otherwise, using it results in undefined behavior, which may result in your code misbehaving on startup or exit. There are platforms where it runs with no apparent issues, but you shouldn't rely on that being true.
5.1.2.2.3 Program termination
1 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.
11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main
will have ended in the former case, even where they would not have in the latter.
C programs return a status code to the runtime environment - on *nix and similar platforms, a return code of 0 indicates successful, normal program termination. stdlib.h defines the macros EXIT_SUCCESS and EXIT_FAILURE, which should be used instead of literal numeric values:
#include <stdlib.h>
...
int main( void )
{
...
if ( something_bad_happens )
return EXIT_FAILURE;
...
return EXIT_SUCCESS;
}
Even then, I wouldn't use it, because it's guaranteed to be non-portable.
The difference between them is that void main() is not a valid signature for the main function according to the standard. According to the standard, there are only two valid signatures, and those are:
int main(void)
int main(int argc, char *argv[])
A compiler may allow other signatures, but it is not required by the standard. One common non-standard signature is:
int main(int argc, char *argv[], char *env[])
The return statement simply returns an integer that can give information to the caller. If you're in a linux environment, you can inspect this value like this:
$ ./a.out
$ echo $?
In the bash shell, the variable $? contains the return value of the last exectuded command.
You can read more about main signatures and return values here: https://port70.net/~nsz/c/c11/n1570.html#5.1.2
int main is the only valid way to declare the main() function, the others are wrong. You can optionally specify parameters int main(int argc, char *argv[]), but the return type int is required.
The return value of the main() function is used to tell the calling application how the program ended. Returning 0 means that the program completed successfully, while non-zero values are used to indicate some kind of error. This is typically tested in shell scripts. You can also call the exit() function to end the program, its argument will be used as the exit status the same way (this allows terminating the program from inside functions).
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 7 years ago.
Can anyone tell me the difference between int main() and int main(void)? Why do both of them work and what is the default argument to int main()?
No difference under ordinary circumstances. This is no 'default argument to main()', since it has no arguments at all.
Here's the un-ordinary circumstance. If you write your own call to main, then () will permit you to pass it any parameters you like, while (void) will force you to pass it none. Still, none of this matters in terms of the 99.99999999% case, which is main being invoked by the runtime to launch your program. The runtime neither knows nor cares if you write () or (void).
If you code the standard int main(int argc, char **argv) you will get your command-line parameters in there.
main() allows you to call main with any number of parameters. main(void) forces you to call main with no parameters. So:
main(foo, bar);
Is fine with main() but not with main(void) - the compiler generates an error.
Now if you're specifically asking about the program's entry point, it doesn't really make a difference; in either case, you won't have the arguments to the program (argc, argv, envp) available.
From a practical viewpoint, there's no real difference. With int main(void), you're explicitly stating that main takes no parameters, so you can't invoke it with any. With int main(), you're leaving open the possibility of invoking main with some parameters.
However, except in strange situations like code golf or intentionally obfuscated code, you don't invoke main anyway -- it's the entry point to the program, so it's automatically invoked by the startup code. The startup code will pass the command line arguments anyway, so your choices don't change how it's invoked, only whether you use or ignore the parameters that get passed.
The standard does specifically allow you to define main with or without parameters (§5.1.2.2.1/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;
Though it's outside the tags specified, in C++ the situation is slightly different. In C, a function declaration like:
int f();
specifies that f is a function returning an int, but gives no information about the number or type of parameters f might expect (this is included primarily for compatibility with old code -- at one time, this was the only way to declare a function in C). In C++, that same declaration explicitly declares f as a function that takes no parameters, so attempting to invoke f with one or more parameters cannot invoke this function (it must either invoke another overload or produce an error if no suitable overload is found).
People use void main() /*empty parens ()*/
I have been taught to write void main(void)
Any ideas what the difference is?
I'm not sure what the standards are nowadays, but in traditional ANSI C, using empty parentheses indicates that the function can take any number of arguments. Declaring a void parameter on the other hand indicates that the function only takes zero arguments. In this case (and many others), it really doesn't matter too much.
If you want to be strict though, it's probably best to define the void parameter. Of course, the main function can also be defined as int main(int argc, const char* argv[]) - which is perfectly valid, but often unnecessary if you don't care about arguments.
From the C99 standard:
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; or in some other implementation-defined manner.
When main is defined without parameters, will argc and argv still be present on the stack?
These prototypes of main() are both non-standard.
Precision on that question can be found on the comp.lang.c faq : http://c-faq.com/decl/main.html
EDIT: changed "wrong" to "non-standard" as the norm allows implementation-defined prototypes.
There is no difference but usually main should return int. Some compilers will give you a warning (at least the GNU compiler - gcc):
$ cat x.c
void main(void){}
$ gcc x.c
x.c: In function `main':
x.c:1: warning: return type of 'main' is not `int'
As mentioned the prototype of main is (according to standard):
int main(int argc, const char* argv[])
main is a function, as other function. Almost. Anyway, being a function, it is called by some other code (a start up code). Usually (read: almost always) int main() is the correct one, but indeed what is the real correct one depends on the platform you are working it. Since, as said, main function could be called by a startup code that pass in no arguments at all, and that expect no a return value in a specific register (so that void main(void) is correct).
The int main() is correct since normally start up code expect a return value, and pass in two arguments. By saying int main(void) you are saying main takes no argument at all, that is false in most cases. With () you say there are arguments (one, two, three, you don't care), but you are not interested in them, so you are not interested in saying what they are and which type they are.
As I can see in codes, the most used prototype for "normal" environments (no embedded device or other "strange" environments where main can be called differently) is int main() when you disregard the passed int argc, char **argv arguments. (GCC complain since we are using a version for gcc suitable for the enviroment; test it with cross GCC version for one of the environment where startup code does not pass any arguments and expect no a return value)
edit
Just to be kind to skeptical persons; on the an environment where the main function is called, with two arguments, the following
int func()
{
return 0;
}
int func2(void)
{
return 1;
}
int main(void)
{
int a;
a = func(a, a); /* A */
a = func2(a); /* B */
return 0;
}
says no error for A, while for B says too many arguments to function ‘func2’, compiled with gcc -std=c99 -pedantic. Changing int main(void) into int main() makes no difference, and no warnings.
On other evironments (I can't do practical tests now), void main(void) is ok, while in this case it raises a warning. The warning is not because of standard alone, but only since in the environment in use the prototype for main does not match. Standard seems to allow any other "configuration" for main.
In the OP case, considerering the "normal" enviroment (O.S. like GNU/Linux e.g.), where two args are passed to the main, and a return value is expected, the int main() is preferable (arguments are pushed on the stack by the startup code whether you say int main(void) or not, so int main() to me make more sense)
edit
One more note, always for skeptical person. As already proved, B raises an error, since I've said that it is int func2(void) but I call it passing an argument. Then, let us suppose we can compile the startup code and link it, as any other code. Somewhere, it will call the main, in a way like
retval = main(argc, argv);
If we used int main(void), the compiler will stop, giving an error, since startup code (in this environment) is trying to call main with two arguments. If we use int main() nothing happens and the code gets compiled correctly.
So, int main() is superior to int main(void) (in environment where we expect two arguments to main possible)
edit
More likely the call is like
retval = main(_argc, _argv, environ);
on many systems, but this does not change the previous speech.
final edit
Did anyone find that when building a command line tool (i.e. on systems where int argc, char ** makes sense) with int main(void), the chosen compiler/linker links a startup code where the main is called without arguments (whatever the calling conventions are), and instead when building with int main(int argc, char **argv) the startup code is different and in fact calls the main with those two arguments (even if the main itself doesn't use them)?