我知道要在 C 中使用命令行参数,您需要执行以下操作:
int main(int argc, char **argv)
{
//use argc and argv
}
今天我在想它,我意识到我从来没有见过它们叫做 argc
和 argv
。是要求他们这样称呼还是只是约定俗成?如果需要,为什么会这样?
我可以这样做吗:
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表示 argc
和 argv
按照约定命名(分别为参数计数和参数 vector )。
关于c - 传递给 main() 的参数名称是否重要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26874309/