Home » C++ » Is it safe to rename argc and argv in main function?

Is it safe to rename argc and argv in main function?

Posted by: admin November 30, 2017 Leave a comment

Questions:

A lot of programs use standard names for a number of arguments and arrays of strings. The prototype of main function looks like: int main(int argc, char *argv[]);. But would I break something if I choose custom names for these variables?

E.g. int main(int n_of_args, char *args[]);

In the context of the compiler, everything is fine. These variables are local for main function, so they may have any names. And the simple code builds and runs perfectly. But these names may be used by preprocessor. So is it safe to rename these arguments?

PS Personally I find these names bad, because they look very similar and differ in only one letter. But EVERYONE uses them for some kind of reason.

Answers:

Yes, it is safe, so long as you use valid variable names. They’re local variables, so their scope doesn’t go beyond the main function.

From section 5.1.2.2.1 of the C standard:

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

That being said, using anything other than argc and argv might confuse others reading your code who are used to the conventional names for these parameters. So better to err on the side of clairity.

Questions:
Answers:

Sure you can rename these parameters safely as you like

 int main(int wrzlbrnft, char* _42[]) {
 }

Names are written in sand. They don’t have any influence on the finally compiled code.


The only thing that matters is, that parameter types of declaration and definition actually match.

The signature of the main() function is intrinsically declared as

 int main(int, char*[]);

if you need to use them in an implementation actually you’ll need to name them. Which names are used is actually irrelevant as mentioned before.

Questions:
Answers:

Yes. It is safe, it looks weird, but it won’t break anything.

Questions:
Answers:

The names argc and argv were actually mandated by the C++ standard prior to C++11. It stated:

All implementations shall allow both of the following definitions of main:

int main ()

and

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

and went on to discuss the requirements on argc and argv.

So technically, any program using different names was not standard-conforming, and the compiler was allowed to reject it. No compiler actually did so, of course. See this thread on comp.std.c++, or section 3.6.1 of this C++03 draft standard.

This was almost certainly a mere oversight, and was changed in C++11, which instead says

All implementations shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main (8.3.5). In the latter form, for purposes of
exposition, the first function parameter is called argc and the second
function parameter is called argv,…

Questions:
Answers:

Yes, it is safe to use different names.

Personally, I wouldn’t recommend it, though, as the traditional argc and argv are so widely known and familiar to every other C programmer who might ever work with your code. In the long run, using your own, special, different names will cause far more confusion and/or frustration among your readers than it will ever save you because you like your names better.

“When in Rome, do as the Romans do.”

Questions:
Answers:

Yes you can rename them as you want. They are simply function parameter names, nothing more.