c - 传递给 main() 的参数名称是否重要

标签 c command-line-arguments argv argc

我知道要在 C 中使用命令行参数,您需要执行以下操作:

int main(int argc, char **argv)
{
//use argc and argv
}

今天我在想它,我意识到我从来没有见过它们叫做 argcargv。是要求他们这样称呼还是只是约定俗成?如果需要,为什么会这样?

我可以这样做吗:

int main(int length, char**myinput){
//use length as you would argc and myinput
// as you would argv
}

我还没有机会编写程序来测试它。

最佳答案

C 标准规定您可以随意调用它们。

5.1.2.2.1 Program startup

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:

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): [..]

What does int argc, char *argv[] mean? , 回答者 meager表示 argcargv 按照约定命名(分别为参数计数和参数 vector )。

关于c - 传递给 main() 的参数名称是否重要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26874309/

相关文章:

Python C 扩展回调函数

c++ - 什么时候不使用开源代码?

c++ - C/C++ 的选项解析器?

perl - 将参数传递给脚本,得到 : "Use of uninitialized value $ARGV[0]"

c - 从数组中删除前 X 个元素

c - 为什么我在这里遇到段错误? [C]

c# - 在命令行参数中传递新行?

ctype.h isdigit 不适用于 argv

清理 argv 程序

将数组中的 char 元素与运算符进行比较