70

What really are the valid signatures for main function in C? I know:

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

Are there other valid ones?

Solomon Ucko
  • 2,682
  • 2
  • 14
  • 27
Prady
  • 9,658
  • 38
  • 120
  • 170
  • 1
    What version of C? Old versions of compilers allow all kinds of things. – Mr. Boy Jan 21 '10 at 09:49
  • The OP should clearly state what he means as the mysterious C moniker. Standard C? Which standard of C? – mloskot Jan 21 '10 at 12:22
  • 5
    I tend to assume when someone talks about C, they mean ISO C. If they leave off the version, I assume the current C99 but still give info about c1x if it's relevant. – paxdiablo Jan 21 '10 at 13:13
  • 2
    In September 2013, this question was closed as a duplicate of [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c), but it was reopened in July 2017 after a gap of almost 5 years. The information in the answers here is repeated in the answers to that question. – Jonathan Leffler Sep 13 '18 at 23:43
  • There is also another question to which this one was once duplicated: [What is the proper declaration of `main()`?](https://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main), though that was created after this question, and is strictly a C++ question, so it isn't all that appropriate as a duplicate for this. – Jonathan Leffler Sep 13 '18 at 23:53

5 Answers5

78

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.

Community
  • 1
  • 1
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • 2
    How about `int main(int argc, const char* argv[]);`? – potrzebie Aug 15 '13 at 19:39
  • 11
    @potrzebie According to the standard, section 5.1.2.2.1: "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, [...]". Thus it would seem that const in the signature is invalid. – arvidj Jul 08 '14 at 13:57
  • To make your answer more future proof, please mention what is the "current standard". – Cristian Ciupitu Nov 23 '14 at 18:39
  • @Christian,I did mention that in the question, referring to the text in C11 and noting C99 was almost identical. But I'll reiterate (preiterate?) it in the first paragraph as well, as per your suggestion. Cheers. – paxdiablo Nov 23 '14 at 21:43
  • @potrzebie One could probably defend `int main(int argc, char* const argv[]){...}`, making the *parameter itself* const. I'd think it's defensible because it is compatible with an assumed implicit declaration `int main(int argc, char* argv[]);`. – Peter - Reinstate Monica Jun 19 '19 at 16:09
  • @Peter, I'm not *totally* sure that would be valid under the "or equivalent" rule. Since the non-const variant would allow you to change `argv[3]` to point to a totally different string, that ability is lost if you `const` the individual pointers. I suspect it would be no different to `const char *argv[]` which would lose you the ability to chage characters withing the arguments (like `argv[3][1] = '\0'` to force it to only the first character). – paxdiablo Jun 20 '19 at 02:36
  • @Peter, specifically, the text "`argc` and `argv` and the strings pointed to by the `argv` array shall be modifiable by the program" might come into play, though it *could* be read as "only `argv` and the characters pointed to by `argv[N]`, *not* the individual pointers that make up `argv`". This is why sloppy language has no place in standards :-) – paxdiablo Jun 20 '19 at 02:45
  • @paxdiablo I think we agree `argv` (proper) is certainly modifiable (since it is, like all function parameters, a local copy) -- but a function *implementation* is free to declare that it refrains from modifying this local variable by declaring it const, without changing the function signature. The standard means that argv does not point to const memory, and neither do the pointers in that non-const memory (i.e. that we can say `++argv` goes without saying, but the standard mandates that we can also say `++*argv` and even `++**argv` (if argc > 0). – Peter - Reinstate Monica Jun 20 '19 at 05:32
21

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.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • *but that only matters if you call it recursively.* Fun fact: calling `main` from inside the program is UB in C++; compilers are allowed insert calls to static constructors into the top of main, or whatever else. I think it is allowed in C, though. (But you might find that ICC resets the FP environment / rounding modes because it does call an Intel init function from the top of main.) – Peter Cordes Jun 19 '19 at 00:57
  • It is very difficult to find exactly in `C11` the text that allows you to say (the bolded phrase): _"NB: The C11 standard illustrates both int main() and int main(void) in different examples —_ ***both are valid in C,***". I do see that exact syntax in `C11`, but have not found content that states explicitly that it is valid. But then I am not experienced in properly interpreting things I see in the standard. Are we to interpret that if a particular syntax exists as an illustration in the this standard, that it is to be considered legal (or valid)? – ryyker Sep 16 '20 at 15:42
  • 1
    It's not easy, @ryyker, to find the information, but in C11, [§6.7.6 Declarators](http://port70.net/~nsz/c/c11/n1570.html#6.7.6) shows that an empty argument list is valid for a function, and [§6.9.1 Function definitions](http://port70.net/~nsz/c/c11/n1570.html#6.9.1) uses the _declarator_ notation from §6.7.6. The counter to the general 'function definition' material is [§5.1.2.2.1 Program startup](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1), shown in this answer. The "or equivalent" clause and its footnote are ambiguous, but `int main() { … }` is equivalent to `int main(void) { … }`. – Jonathan Leffler Sep 16 '20 at 16:26
  • 1
    Note that `int main();` and `int main(void);` are _not_ equivalent. Both declare (rather than define) a function, but the former specifies nothing about the parameter list (does not provide a prototype for the function), whereas the latter explicitly says "the function takes no arguments". (The difference between declaration and definition is why the previous comment has `int main() { … }`, indicating the definition of the function, whereas here the `{ … }` is replaced by a semicolon, indicating a declaration of the function.) – Jonathan Leffler Sep 16 '20 at 16:29
  • Thanks Jonathan - Your clarity in unwinding difficult content is very helpful. I do wonder though in the last statement of your first response you say: _but `int main() { … }` is equivalent to `int main(void) { … }`_. Then in the first statement in your second comment you are seeming to make the opposite assertion: _Note that `int main();` and `int main(void);` are not equivalent_. I believe that overall I have interpreted that the two are _not_ equivalent, as one ( `int main();` ) can be demonstrated to accommodate multiple arguments, and the other will fail to compile with any arguments. – ryyker Sep 16 '20 at 16:45
  • 1
    Hmm — yes, strictly, the `int main() { … }` and `int main(void) { … }` are not equivalent because the former still doesn't provide a prototype for `main()` whereas the latter does. Both do, however, define a function that takes no arguments (and in that sense, they're equivalent — which is what I should have said, but the space was lacking in a comment). The only time the difference matters is if your code calls `main()` recursively (or messes around with function pointers to `main()`) — neither of which is an everyday occupation for C programmers (and recursive calls are verboten in C++). – Jonathan Leffler Sep 16 '20 at 16:48
  • These comments (and answer) are shining a very useful light on a sometimes contentious topic. I now have it marked as a reference :) Thank you again. – ryyker Sep 16 '20 at 17:22
  • 1
    @ryyker I believe that this case is covered by [§6.7.6 Declarators, semantics, p14](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p14) *"...An empty list in a function declarator that is part of a **definition** of that function specifies that the function has no parameters..."* which has a note, [145](http://port70.net/~nsz/c/c11/n1570.html#note145) which leads to [6.11.6](http://port70.net/~nsz/c/c11/n1570.html#6.11.6): *"The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an **obsolescent** feature."*. – Bob__ Sep 16 '20 at 18:47
  • I just can say THANKS. This is a very exhaustive explanation, esp. regarding the common extensions to the third argument possible. If I could I would have voted this up twice! – math Mar 10 '21 at 16:17
8

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.

unwind
  • 364,555
  • 61
  • 449
  • 578
  • 10
    This is not quite correct. Execve takes an environment argument, but this has nothing to do with the calling convention for main. Rather it's used to initialize `extern char **environ;`. – R.. GitHub STOP HELPING ICE Jun 02 '11 at 12:53
  • @R..: In practice many C implementations on POSIX systems *do* pass a 3rd `envp` arg to `main`. I'm not sure whether POSIX itself specifies this as a 3rd valid signature for `main` or not. You can verify that it does work in practice on GNU C with this program: https://godbolt.org/z/9lie95 (it passes its argv and envp to `execve("/usr/bin/env")`, so you can see that it did inherit a sane environment instead of returning `-EFAULT`). But yes, this answer describes it wrong, implying that the existence of execve implies a new signature for `main`. – Peter Cordes Jun 19 '19 at 00:53
8

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.

kennytm
  • 469,458
  • 94
  • 1,022
  • 977
3
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()

flashnik
  • 1,830
  • 4
  • 18
  • 37