Reputation: 542
Can you define main() to take multiple arguments, for example:
int main(int argc1, int argc2, char* argv1[], int* argv2[])
Upvotes: 1
Views: 1118
Reputation: 263617
In C, the only portable definitions for main
are:
int main(void) { /* ... */ }
and
int main(int argc, char *argv[]) { /* ... */ }
or equivalent. Other implementation-defined forms are permitted -- but as the term "implementation-defined" implies, they're permitted only if your compiler chooses to permit them.
There are some common extensions, such as:
int main(int argc, char *argv[], char *envp[]) { /* ... */ }
but I doubt that any compiler supports the particular form you suggest. I'm not sure how it would be useful; the existing argc
/argv
form allows arbitrarily many command-line arguments.
C++ is slightly different from C in that it requires the return type to be int
; in C, a compiler may, but need not, permit other return types. Also, in C++ ()
means that a function has no parameters, equivalent to C's (void)
. (C++ also permits (void)
for C compatibility.)
Upvotes: 8
Reputation: 60999
[basic.start.main]/2:
An implementation shall not predefine the
main
function. This function shall not be overloaded. It shall have a declared return type of type int, but otherwise its type is implementation-defined. An implementation shall allow both
- a function of () returning
int
and- a function of
(int
, pointer to pointer tochar)
returningint
as the type of
main
(8.3.5).
An implementation could theoretically allow the signature mentioned in the question - but it would be quite nonsensical, and it seems that no relevant compiler supports it. (What would it pass?)
Although there are certain signatures of main
that are indeed supported and not covered by standard, e.g.:
int main( int, char**, char** )
Upvotes: 1
Reputation: 57749
Yes, you can have the main
function signature any way you want it; but it will not be standard.
You will have to change the compiler to accept your new declaration. And emit the proper code for it.
You may have to change the operating system to correctly pass the arguments from the User to your program.
Again, yes, other declarations are possible, but they will not be standard and require modifications to OS and tool chain.
Upvotes: 1
Reputation: 141648
Your compiler gets to make the decision of what signatures (and return type) of main
are allowed, and what the behaviour of the program is for each one. Consult its documentation to find out about this.
The only signatures that all compilers must support are the two standard ones (and equivalent):
int main(void)
int main(int, char **)
Upvotes: 1