ARGc和ARGv中的ARG指的是"参数"(外语:ARGuments, argument counter 和 argument vector )
#include <stdio.h>//#包含<stdio.h>
int main(int argc,char* argv[]) //整数类型主函数(整数类型统计参数个数,字符类型指针数组指向字符串参数)
{
printf("%d\n",argc); //格式化输出
while(argc) //当(统计参数个数)
printf("%s\n",argv[--argc]); //格式化输出
return 0; //返回0;正常退出
}
假设将其编译为 test.exe
在命令行下
> test.exe test hello
得到的输出结果为
3
hello
test
test.exe
main(int argc, char* argv[ ]),其中argc是指变量的个数,本例中即指test和hello这两个变量和程序运行的全路径名或程序的名字,argc即为3。
argv是一个char *的数组,其中存放指向参数变量的指针,此处argv[0]指向test.exe的全路径名或test.exe,argv[1]指向test,argv[2]指向hello。