主函数main中变量(int argc,char *argv[ ])的含义

一直不太理解
int main( int argc, char* argv[] )
或者
int main( int argc, char** argv )

经典例子
对于理解argv[ ]函数很管用:

#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。
再例:

#include<stdio.h>
int main(int argc,char *argv[])
{
    if(argc==1||argc>2)
    printf("请输入想要编辑的文件名如:fillname");
    if(argc==2)
    printf("编辑%s\n",argv[1]);
    return 0;
}

编译该程序:gcc -o edit edit.c
运行:〉edit
结果:请输入想要编辑的文件名如:fillname
运行:〉edit f1.txt
结果:编辑 f1.txt
执行edit时,argc为1,argv[0]指向edit
而执行edit f1.txt时,argc的值为2,argv[0]指向edit,argv[1]指向f1.txt

转自:https://www.jianshu.com/p/77234c1618f9