在主函数中重命名argc和argv是否安全?

时间:2022-10-09 10:46:11

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?

许多程序对许多参数和字符串数组使用标准名称。主函数的原型为:int main(int argc, char *argv[]);但是,如果我为这些变量选择自定义名称,会不会破坏一些东西呢?

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

例如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.

PS:我个人觉得这些名字不好,因为它们看起来很相似,而且只有一个字母不同。但每个人都是出于某种原因使用它们。

10 个解决方案

#1


117  

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:

C标准第5.1.2.2.1节:

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:

程序启动时调用的函数名为main。实现没有声明此函数的原型。它应该定义为返回类型的int,并且没有参数:

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):

或有两个参数(此处称为argc和argv,但可以使用任何名称,因为它们是声明它们的函数的局部):

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.

也就是说,使用除了argc和argv之外的任何东西可能会让其他正在阅读您的代码的人感到困惑,他们已经习惯了这些参数的常规名称。所以最好还是站在原告的立场上犯错。

#2


36  

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

argc和argv实际上是c++标准在c++ 11之前指定的。它说:

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.

接着讨论了argc和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.

因此,从技术上讲,任何使用不同名称的程序都不符合标准,编译器也可以拒绝它。当然,实际上没有编译器这样做。在comp.std上看到这个线程。c++,或本c++ 03标准草案第3.6.1节。

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

几乎可以肯定,这只是一个疏忽,在c++ 11中发生了变化,它说

All implementations shall allow both

所有实现都应该允许这两种实现

  • a function of () returning int and
  • 返回int和的函数
  • a function of (int, pointer to pointer to char) returning int
  • 返回int的函数(int,指向char的指针)

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

作为main的类型(8.3.5)。在后一种形式中,为了便于说明,第一个函数参数称为argc,第二个函数参数称为argv,

#3


28  

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

main()函数的签名在本质上被声明为

 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.

如果您需要在实现中使用它们,实际上您需要为它们命名。所使用的名称实际上与前面提到的不相关。

#4


15  

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

是的。它是安全的,看起来很奇怪,但它不会破坏任何东西。

#5


7  

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.

就我个人而言,我不会推荐它,因为传统的argc和argv是所有其他程序员都熟悉和熟悉的,他们可能会使用您的代码。从长远来看,使用你自己的、特别的、不同的名字会给你的读者带来更多的困惑和/或挫败,而这将永远拯救你,因为你更喜欢你的名字。

"When in Rome, do as the Romans do."

“入乡随俗。”

#6


5  

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

是的,你可以按自己的意愿重命名。它们只是函数参数名,仅此而已。

#7


3  

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.

我觉得大家都很好地涵盖了c++技术规则:答案是肯定的。让我们抛开传统,这1个特殊的函数是特殊的和具有代表性的,它包含了在这个基础上不改变的有效点。

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.

对我来说,这个问题涉及到用代码表达英语的选择。你似乎被简短的文字描述所困扰,尤其是,如果短的文字看起来很相似的话。但是,在您的示例中,将argn更改为n_of_args只完成了将一种类型的短线转换为另一种形式的速记,没有添加真正的值:澄清或其他可见属性。

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

“数字”一词已被一个字母“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 )

主(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.

我总是在想两件事:根据它们是什么和/或它们的隐含用法来命名。称它为argumentVector对我来说是多余的,因为作为向量的性质是由双重间接**暗示的。因此,更好的方法是:**参数。

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

有些人会说,名为argumentCount的变量被声明为int类型,一个Count不能为负,但是可以有一个负的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.

同样,它是什么以及它是如何被使用的在这个解释中发挥作用。如果它是一个计数,那么我假设它永远不会是负的。毕竟,你怎么能数-2个苹果。我想说,你欠两个苹果。如果它是一个数字,那么我期望一个负数的情况是可能的。这就是为什么“of”这个词对你来说可能很重要。这一点,或者一个集合所指的数字意味着一个特定的项,而不是集合本身的属性。即:argumentsNumber = 5表示一个特定的参数,而不是多个参数。

main( int maxArgumentsIndex, char ** arguments ).

main(int maxArgumentsIndex, char * parameters)。

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!!

这消除了歧义。称它为索引可以消除否定的情况歧义,还可以描述它是什么,以及如何使用它。它还暗示了英语中max是绝对的,编写修改这个值的代码会感到奇怪(应该是const)。“argument”在这里是有意义的,因为它是复数的,描述了它是什么,以及它应该如何使用。即使用这种方式解释也很危险,因为索引是Count/NumberOf的-1。5个参数产生4的maxIndex !

Any other function and I would completely use:

任何其他功能,我将完全使用:

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

无效函数(const unsigned int maxArgumentsIndex, const char * parameters)

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.

并不是所有的情况都值得用长时间描述。事实上,有时短的手会产生更强的可读性,尤其是在编写数学类的时候,比如Vec3f、Matrix、Quaternion等等……我几乎总是试着去匹配数学语言而不是语言。浮动x y z vrs。浮动xComponent等等。

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 ;)

我知道这一切都是一种风格的选择,但从长远来看,意识到这些选择真的会有所帮助。我敢保证,当数组不是以复数形式编写时,经验丰富的程序员一定会感到烦恼,但话说回来,main是一种特殊的存在形式;

#8


2  

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

按照C标准,是的,您可以重命名,不会有任何影响。正如我所理解的,在C语言中,默认的关键字/类型/令牌名是用目的/用法来定义的,所以用同样的方法定义名称

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.

在GCC中,程序执行以函数名main开始,它不依赖于他的参数。

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.

当您为微控制器编写独立的程序时,您不需要为名称main操心,而是可以定义您自己的名称并更改程序集entry_point以指向您的函数。它依赖于控制器编译器和预先定义控制器源代码的可用性。我在《代码武士》下的飞思卡尔控制器中做过这个。

My Note:

我的注意:

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

最好遵循通用的标准/代码风格,使代码更清晰易读。

#9


0  

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.

但我不认为我需要告诉你这种做法有多糟糕。

#10


-1  

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

如果您不喜欢变量名,为什么不使用宏#define:

#define yourCounterName argc
#define yourVectorName  argv 

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

你将不会承担任何风险并产生一个“干净的解决方案”。

#1


117  

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:

C标准第5.1.2.2.1节:

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:

程序启动时调用的函数名为main。实现没有声明此函数的原型。它应该定义为返回类型的int,并且没有参数:

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):

或有两个参数(此处称为argc和argv,但可以使用任何名称,因为它们是声明它们的函数的局部):

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.

也就是说,使用除了argc和argv之外的任何东西可能会让其他正在阅读您的代码的人感到困惑,他们已经习惯了这些参数的常规名称。所以最好还是站在原告的立场上犯错。

#2


36  

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

argc和argv实际上是c++标准在c++ 11之前指定的。它说:

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.

接着讨论了argc和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.

因此,从技术上讲,任何使用不同名称的程序都不符合标准,编译器也可以拒绝它。当然,实际上没有编译器这样做。在comp.std上看到这个线程。c++,或本c++ 03标准草案第3.6.1节。

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

几乎可以肯定,这只是一个疏忽,在c++ 11中发生了变化,它说

All implementations shall allow both

所有实现都应该允许这两种实现

  • a function of () returning int and
  • 返回int和的函数
  • a function of (int, pointer to pointer to char) returning int
  • 返回int的函数(int,指向char的指针)

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

作为main的类型(8.3.5)。在后一种形式中,为了便于说明,第一个函数参数称为argc,第二个函数参数称为argv,

#3


28  

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

main()函数的签名在本质上被声明为

 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.

如果您需要在实现中使用它们,实际上您需要为它们命名。所使用的名称实际上与前面提到的不相关。

#4


15  

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

是的。它是安全的,看起来很奇怪,但它不会破坏任何东西。

#5


7  

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.

就我个人而言,我不会推荐它,因为传统的argc和argv是所有其他程序员都熟悉和熟悉的,他们可能会使用您的代码。从长远来看,使用你自己的、特别的、不同的名字会给你的读者带来更多的困惑和/或挫败,而这将永远拯救你,因为你更喜欢你的名字。

"When in Rome, do as the Romans do."

“入乡随俗。”

#6


5  

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

是的,你可以按自己的意愿重命名。它们只是函数参数名,仅此而已。

#7


3  

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.

我觉得大家都很好地涵盖了c++技术规则:答案是肯定的。让我们抛开传统,这1个特殊的函数是特殊的和具有代表性的,它包含了在这个基础上不改变的有效点。

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.

对我来说,这个问题涉及到用代码表达英语的选择。你似乎被简短的文字描述所困扰,尤其是,如果短的文字看起来很相似的话。但是,在您的示例中,将argn更改为n_of_args只完成了将一种类型的短线转换为另一种形式的速记,没有添加真正的值:澄清或其他可见属性。

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

“数字”一词已被一个字母“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 )

主(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.

我总是在想两件事:根据它们是什么和/或它们的隐含用法来命名。称它为argumentVector对我来说是多余的,因为作为向量的性质是由双重间接**暗示的。因此,更好的方法是:**参数。

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

有些人会说,名为argumentCount的变量被声明为int类型,一个Count不能为负,但是可以有一个负的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.

同样,它是什么以及它是如何被使用的在这个解释中发挥作用。如果它是一个计数,那么我假设它永远不会是负的。毕竟,你怎么能数-2个苹果。我想说,你欠两个苹果。如果它是一个数字,那么我期望一个负数的情况是可能的。这就是为什么“of”这个词对你来说可能很重要。这一点,或者一个集合所指的数字意味着一个特定的项,而不是集合本身的属性。即:argumentsNumber = 5表示一个特定的参数,而不是多个参数。

main( int maxArgumentsIndex, char ** arguments ).

main(int maxArgumentsIndex, char * parameters)。

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!!

这消除了歧义。称它为索引可以消除否定的情况歧义,还可以描述它是什么,以及如何使用它。它还暗示了英语中max是绝对的,编写修改这个值的代码会感到奇怪(应该是const)。“argument”在这里是有意义的,因为它是复数的,描述了它是什么,以及它应该如何使用。即使用这种方式解释也很危险,因为索引是Count/NumberOf的-1。5个参数产生4的maxIndex !

Any other function and I would completely use:

任何其他功能,我将完全使用:

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

无效函数(const unsigned int maxArgumentsIndex, const char * parameters)

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.

并不是所有的情况都值得用长时间描述。事实上,有时短的手会产生更强的可读性,尤其是在编写数学类的时候,比如Vec3f、Matrix、Quaternion等等……我几乎总是试着去匹配数学语言而不是语言。浮动x y z vrs。浮动xComponent等等。

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 ;)

我知道这一切都是一种风格的选择,但从长远来看,意识到这些选择真的会有所帮助。我敢保证,当数组不是以复数形式编写时,经验丰富的程序员一定会感到烦恼,但话说回来,main是一种特殊的存在形式;

#8


2  

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

按照C标准,是的,您可以重命名,不会有任何影响。正如我所理解的,在C语言中,默认的关键字/类型/令牌名是用目的/用法来定义的,所以用同样的方法定义名称

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.

在GCC中,程序执行以函数名main开始,它不依赖于他的参数。

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.

当您为微控制器编写独立的程序时,您不需要为名称main操心,而是可以定义您自己的名称并更改程序集entry_point以指向您的函数。它依赖于控制器编译器和预先定义控制器源代码的可用性。我在《代码武士》下的飞思卡尔控制器中做过这个。

My Note:

我的注意:

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

最好遵循通用的标准/代码风格,使代码更清晰易读。

#9


0  

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.

但我不认为我需要告诉你这种做法有多糟糕。

#10


-1  

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

如果您不喜欢变量名,为什么不使用宏#define:

#define yourCounterName argc
#define yourVectorName  argv 

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

你将不会承担任何风险并产生一个“干净的解决方案”。