C/C++生成的exe文件如何传参数到main中

Main函数参数argc,argv说明:

C/C++语言中的main函数,经常带有参数argc,argv,如下: 

 
  1. int main(int argc, char** argv)

  2. int main(int argc, char* argv[])

这两个参数的作用是什么呢?argc 是指命令行输入参数的个数,argv存储了所有的命令行参数。假如你的程序是hello.exe,如果在命令行运行该程序,运行命令为: 
hello.exe Shiqi Yu
那么,argc的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。   

下面的程序演示argc和argv的使用:  

 
  1. #include <stdio.h>

  2. int main(int argc, char ** argv)

  3. {

  4. int i;

  5. for (i=0; i < argc; i++)

  6. printf("Argument %d is %s.\n", i, argv[i]);

  7. return 0;

  8. }

假如上述代码编译为hello.exe,那么运行 
hello.exe a b c d e
将得到  
Argument 0 is hello.exe.
Argument 1 is a.
Argument 2 is b.
Argument 3 is c.
Argument 4 is d.
Argument 5 is e.
运行  
hello.exe lena.jpg
将得到  
Argument 0 is hello.exe.

Argument 1 is lena.jpg.

reference:

http://topic.csdn.net/u/20080622/09/6447dfb1-e34e-4e57-8b30-d9f304d8b626.html

参考文献:

https://blog.csdn.net/peerlessbloom/article/details/7039925

猜你喜欢

转载自blog.csdn.net/weixin_41282397/article/details/83629210