其中第一个表示参数的个数;第二个参数中argv[0]为自身运行目录路径和程序名,argv[1]指向第一个参数、argv[2]指向第二个参数……
测试例子:
#include <stdio.h>#include <string.h>
int main(int argc,char *argv[])
{
printf("argc= %d\n",argc)
while(argc--)
{
printf("%s \n",*argv++)
}
return 0
}
C语言的带参数的main函数格式为main(int argc,char* argv[]),其中argc为参数个数加1,argv为参数列表,从argv[1]开始。编译后生成可执行文件,可以在执行的程序后加参数,比如编译一个程序为test.exe,则在执行时可以输入:test.exe 1 firestone
在这里,argc等于3,argv[0]为"test.exe",argv[1]为"1",argv[2]为"firestone"。
如果有下面的程序:
#include <stdio.h>
void main(int argc,char *argv[])
{
printf("You've input %d parameters.\n",argc-1)
for(int i=1i<argci++)printf("The No.%d is:%s\n",i,argv[i])
}
则上面的输入会产生如下运行结果:
You've input 2 parameters.
The No.1 is 1
The No.2 is firestone