The meaning and usage of parameters argc and argv in C++ main function

argc is the abbreviation of argument count, which indicates the number of parameters passed into the main function;

argv is the abbreviation of argument vector, which represents the parameter sequence or pointer passed into the main function, and the first parameter argv[0] must be the name of the program, and contains the full path where the program is located, so to be precise, we need to input The number of parameters of the main function should be argc-1;

Simple usage example, new project key code:

[cpp]  view plain copy  
  1. #include <iostream>  
  2.   
  3. usingnamespace std;   
  4.   
  5. void main(int argc,char *argv[])  
  6. {  
  7.     for(int i=0;i<argc;i++)  
  8.     {  
  9.         cout<<"argument["<<i<<"] is: "<<argv[i]<<endl;  
  10.     }  
  11.     system("pause");  
  12. }  

argv is a pointer to a pointer, the second parameter of the main function "char *argv[]" can also be replaced with "char **argv" , the two are equivalent.

Press F5 in the compilation environment to run, the output is as follows:


It can be seen that the first variable argv[0] that saves the program name still exists when no parameters are passed in.

There are two ways to pass parameters to the main function. The first way is to set it in the compilation environment. Taking vs2012 as an example, right-click on the project -> Properties -> Configuration Properties -> Debugging -> Command Parameters, enter in the command parameters, Separate each parameter with a space.


After that, click OK and apply, and it will display as follows after running:


The second way and the way that is often used is to pass in through the command prompt. First, you need to open the command prompt window, click the start menu and enter the command "cmd" in "Search programs and files" or directly press the shortcut key Windows+R , and enter "cmd" in the pop-up dialog box to open the command prompt window. :


After opening the command prompt window, you need to enter the full path of the generated exe file. An easy way is to drag the exe file directly into the prompt window , then enter the incoming parameters, separated by spaces, and then press Enter, the display is as follows :


If you insist on entering the full path manually, you will find that after you "Ctrl+C" the path, you cannot paste it by pressing "Ctrl+V" in the prompt window. At this time, you can right-click in the window and try , you will find that the paste function in the right-click menu is still valid .

The next example demonstrates using opencv to display an image:

[cpp]  view plain copy  
  1. #include <iostream>  
  2. #include <core/core.hpp>  
  3. #include <highgui/highgui.hpp>  
  4. usingnamespace std;   
  5. usingnamespace cv;   
  6.   
  7. void main(int argc,char **argv)  
  8. {  
  9.     Mat image=imread(argv[1]);  
  10.     imshow("Lena",image);  
  11.     waitKey();  
  12. }  
注意读入的参数是argv[1],在命令提示符窗口运行:


最后说明一下:一般编译器默认使用argc和argv两个名称作为main函数的参数,但这两个参数如此命名并不是必须的,你可以使用任何符合C++语言命名规范的变量名作为入参,效果是一样的:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <core/core.hpp>  
  3. #include <highgui/highgui.hpp>  
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. void main(int value,char **point)  
  8. {  
  9.     for(int i=0;i<value;i++)  
  10.     {  
  11.         cout<<"argument["<<i<<"] is: "<<point[i]<<endl;  
  12.     }  
  13.     system("pause");  
  14. }  

Original link: http://blog.csdn.net/dcrmg/article/details/51987413

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856833&siteId=291194637