它是安全重命名main函数argc和argv? [英] Is it safe to rename argc and argv in main function?

查看:194
本文介绍了它是安全重命名main函数argc和argv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多程序使用标准地名的一些参数和字符串数组。主要功能的原型如下: INT主(INT ARGC,CHAR *的argv []); 。但我会碰坏,如果我选择了这些变量自定义名称?

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主(INT n_of_args,字符* ARGS []);

在编译器的情况下,一切都很好。这些变量是局部的主功能,因此他们可能有任何名字。而简单code构建并运行完美。但这些名称可以由preprocessor使用。因此,它是安全的重命名这些争论?

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 我个人觉得这些名字不好,因为它们看起来非常相似,在只有一个字母不同。但每个人都使用了他们某种原因。

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.

推荐答案

是的,它是安全的,只要你用有效的变量名。他们是局部变量,因此其范围不超出功能。

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.

C标准第5.1.2.2.1

From section 5.1.2.2.1 of the C standard:

称为在程序启动时的功能被命名为。该
  实施声明没有原型此功能。它应
  与 INT 的返回类型和不带参数定义的:

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) { /*  ... */ }

或两个参数(这里称为 ARGC 的argv 虽然任何
  名称也可以使用,因为它们是局部的,其中它们的功能
  声明

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

话虽这么说,使用其他任何比 ARGC 的argv 可能会混淆别人读你的code谁用于这些参数的常规名称。所以最好还是宁可clairity的一面。

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.

这篇关于它是安全重命名main函数argc和argv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆