Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nameless parameters in main() strictly conforming?

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.

like image 615
user5262423 Avatar asked Jan 30 '26 12:01

user5262423


2 Answers

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

  1. 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

like image 96
Arun Avatar answered Feb 01 '26 07:02

Arun


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() { ... }
like image 42
paxdiablo Avatar answered Feb 01 '26 07:02

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!