C++ allows the following two definitions of main:
int main() { }
int main(int argc, char* argv[]) { }
It also allows char*[] to be char** and argc and argv to be named whatever the programmer wishes. However, does it also allow:
int main(int, char*[]) { }
Is this identical to the above examples? Is it strictly conforming? Note, I don't care if it compiles in your favorite compiler, I'm asking about standards only.
Yes, as stated by @Captain Obvlious, C++ just cares about type of the parameters. C++ standard committee papers publicly available here for your reference.
3.6.1 Main function
- 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
— a function of () returning int and
— a function of (int, pointer to pointer to char) returning int
Yes, it's quite valid.
First, C++11 3.6.1 /2 states the allowable forms of main(), including the two canonical forms:
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[]) { /* ... */ }
Then, in C++11 8.3.5 Functions /11, it states that parameter names are not actually required for function definitions:
An identifier can optionally be provided as a parameter name; if present in a function definition (8.4), it names a parameter (sometimes called "formal argument").
However, given that the lack of names means that you can't access the variables, it's probably a better idea to just use the canonical form without them:
int main() { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With