【C】关于main()函数参数的问题;

main()函数参数的一般形式: int main(int argc, char *argv[])

int argc用来表示参数的数量,argv[]数组是用来存放参数的值;

但是在linux下的gcc编译中传参的时候会出现一点问题,直接上代码好了:

1  #include<stdio.h>
2  int main(int argc, char *argv[])
3  {
4      printf("the first argc is %s\n",argv[0]);
5      printf("the second argc is %s\n",argv[1]);
6      printf("the third argc is %s\n",argv[2]);
7      printf("the fouth argc is %s\n",argv[3]);
8  }

结果如下:

结果把./a.out也作为了一个参数也传了进去,所以第一参数会被认为是./a.out。我们在处理参数的时候需要注意这一点了!

原文地址:https://www.cnblogs.com/ngnetboy/p/2790226.html