在C语言编码过程中,一般情况下我们使用的main函数原型为
int main()
但是main函数完整的原型应为
int main(int argc,char *argv[])
其中,参数argc代表参数的个数,参数argv为命令。
为了验证上述说法,编写如下测试代码test.c
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[])
{
int i,j;
i = atoi(argv[1]);
j = atoi(argv[2]);
printf("the Program name is %s\n",argv[0]);
printf("The command line has %d arguments:\n",argc-1);
printf("argv[1] is %d,argv[2] is %d\n",i,j);
return 0;
}
在Ubuntu16.04下编译并运行
./test 6 8
运行结果如下
the Program name is ./test
The command line has %d arguments:3
argv[1] is 6,argv[2] is 8