Is it safe to rename argc and argv in main function?
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 ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, 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.
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 tochar
) returningint
as the type of
main
(8.3.5). In the latter form, for purposes of exposition, the first function parameter is calledargc
and the second function parameter is calledargv
,…
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.