是什么
简介:
argc : main函数参数个数,当参数为void的时,argc=1,默认参数为可执行文件名
argv : 指针数组,分别指向个参数字符串首地址,其中argv[0]指向默认参数
实例
int main(int argc, char* argv[])
{
int i;
for (i=0; i<argc; i++)
printf("%d: %s\r\n", i+1, argv[i]);
return 0;
}
结果
有什么用
可以向程序传递参数值
举栗子:
#include <stdio.h>
int main( int argc, char *argv[] )
{
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
$./a.out testing
The argument supplied is testing
$./a.out testing1 testing2
Too many arguments supplied.
$./a.out
One argument expected