argc is the abbreviation of argument count, indicating the number of parameters passed into the main function;
argv is the abbreviation of argument vector, indicating the number of parameters passed into the main function Parameter sequence or pointer, and the first parameter argv[0] must be the name of the program, and contains the complete path where the program is located, so to be precise, the number of parameters of the main function that we need to input should be argc-1; Spaces separate parameters, and their length is argc. The array subscript starts from 0, argv[argc]=NULL.
argv[0] points to the full path name of the program when it is run
argv[1] points to the first string after the program name when the program is executed in the command
argv [2] Points to the second string after the execution program name
argv[argc] is NULL
#include <iostream> using namespace std; void main(int argc,char *argv[]) { for(int i=0;i<argc;i++) { cout<<"argument["<<i<<"] is: "<<argv[i]<<endl; } system("pause"); }
argv is a pointer to a pointer, the second parameter of the main function "char * argv[]" can also be replaced by "char **argv", the two are equivalent.
Press F5 to run in the compilation environment, the output is as follows:
Related information: "FAQ"
It can be seen that when no parameters are passed in, the first variable argv[0] that holds the program name still exists.
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 the project->Properties->Configuration Properties->Debug-> ;Command parameters, enter them in the command parameters, and separate each parameter with a space.
Then click OK and apply. After running, the display will be as follows:
The second method, which is also often used, is to pass it 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, 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, and then Enter the incoming parameters, separated by spaces, and then press Enter, as shown below:
The above is the detailed content of How to use argc and argv. For more information, please follow other related articles on the PHP Chinese website!