C语言的带参数的baimain函数格du式为main(int argc,char* argv[]),其中zhiargc为参数个数加1,argv为参数列表,dao从zhuanargv[1]开始。编译后生成可执shu行文件,可以在执行的程序后加参数,比如编译一个程序为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=1;i<argc;i++)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