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

血红的双手。 提交于 2019-11-30 07:47:30

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.

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,…

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.

coredump

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

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

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

I feel that everyone has covered the technical c++ rules well and good: Answer is yes. Let's put aside tradition and the fact that this 1 particular function is special and iconic which contains valid points to not change on this basis.

Often times, I feel the philosophy of the choices are rarely discussed and thus wanted to offer a perspective on this matter as I feel it to be important to the reason why this was asked to begin with.

This question to me involves a choice in expressing english in code in general. You seem to be bothered by short hand descriptions, in particular, if the short hand lands similar looking text. In your example though, changing argn to n_of_args only accomplishes the changing of one type of short hand into another form of shorthand with no real value addition: clarification or other visible properties.

The word 'number' has been replaced by a letter 'n'.

If you are changing a short hand name via the philosophy of anti short hand, then something like this may seem more appropriate:

main( int argumentCount, char ** argumentVector )

I always think about two things: Naming things by what they are and/or by their implied usage. Calling it an argumentVector is redundant to me since the property of being a vector is implied by the double indirection **. Thus, a better long hand for how I would write code is: ** arguments.

Some would say the variable named argumentCount is declared as an int and a Count can not be negative but you can have a negative int {unsigned is better}.

Again, what it is and how it is used comes to play in this interpretation. If it is a Count, then I would assume it would never be negative. After all, how can you have a Count of -2 apples. I would say, you OWE two apples. If it is a Number, then I would expect a negative case to be possible. This is why the additional word 'of' is likely important to you. That, and perhaps a number as referred to by a collection implies a specific item rather than a property of the collection itself. Ie: argumentsNumber = 5 implies a specific argument but not numberOfArguments.

main( int maxArgumentsIndex, char ** arguments ).

This removes ambiguity. Calling it an index removes negative case ambiguity and also describes what it is, and additionaly how to use it. It also implies by the english wording that a max is an absolute and would feel weird writing code that modifies this value (it should be const). 'arguments' makes sense here since it is plural, describes what it is, and how it should be used already. Even interpreting this way can be dangerous as an Index is -1 of a Count/NumberOf. 5 arguments yields a maxIndex of 4!!

Any other function and I would completely use:

void function( const unsigned int maxArgumentsIndex, const char ** arguments )

Not all situations merit long hand descriptors. In fact, some times a short hand yields more readability, in particular, in the case of writing math classes such as a Vec3f, Matrix, Quaternion, etc... I will almost always try to match the math language rather than the linguistic one. float x, y, z vrs. float xComponent and the like.

I understand all of this is a style choice, but being conscious of the choices will really help in the long run. I guarantee seasoned programmers get bothered when arrays are not written in plural form, but then again, main is a special prose of existence ;)

As per C Standards, Yes you can rename, Nothing going to impact. As i understood, in C Language, the default Keyword/types/token names were defined with purpose/usage, so in the same way it is defined names

argc --> argument count

argv --> argument vector

which is also make sense in terms of usage, So you can change to any name expect Reserved names

In GCC, the program execution start with the function name main it doesn't depends on his parameters.

When you write standalone program for micro controllers, you no need to bother about the name main instead you can define your own name and change the Assembly entry_point to point your function. It depends on controller compiler and availability of pre-define controller source code. I've did this in Freescale controller under Code-warrior.

My Note:

It's better to follow the common standards/code style to make code more visible and readable

It is safe as far as the compiler is concerned.

The only problem this can cause is confusion. People who read your code will expect those two variables to have their standard names. You could even do something like this:

int main(int foo, char ** bar)
{
    int argc;
    float argv;

But I don't think I need to tell how bad practice this would be.

If you don't like the variable names, why not substituting them with the macro #define :

#define yourCounterName argc
#define yourVectorName  argv 

You will not take any risk and produce a "clean solution".

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!