C + + 's main function can have no input parameters, or can have input parameters, and can only have two parameters, it is customary to coding as follows:
int Main (intcharint main (intChar* * argv)
where argc = argument count: Indicates the number of array elements passed into the main function, is of type int, and argv = argument vector: Represents an array of pointers passed into the main function, for the char** type. The first array element, Argv[0], is the program name and contains the full path where the program resides. ARGC is at least 1, that is, the argv array contains at least the program name.
#include <iostream>usingnamespace std; int Main (intChar* argv[]) { for (int i=0;i< argc;i++) cout<<argv[i]<<Endl; return 0 ;}
The general compiler defaults to using the ARGC and argv two names as arguments to the main function, but these two arguments are not required, you can use any variable name that conforms to the C + + language naming convention, but to ensure that the first parameter type is int, the second argument is type char**, as shown in.
#include <iostream>usingnamespace std; int Main (intChar* input_parameters[]) { for (int i=0 ; i<count;i++) cout<<input_parameters[i]<<Endl; return 0 ;}
Because the main function cannot be called by other functions, it is not possible to get the actual value inside the program. The parameter value of the main function is obtained from the operating system command line. In the window system, if you compile the linked executable as My_project.exe, at the command prompt (shortcut key windows+r, enter cmd), type the following command (executable parameter parameter parameter ...) :
1996
Will pass three parameters to the main function, the first argv[0] is the file name mentioned earlier, the second argv[1] is "Jisongxie", the third Argv[2] is "1996". In the same vein, more parameters can be passed in. In the Ubuntu system, the same operation can be done through the terminal.
The passed-in parameter array type is char * string type and can be converted by the Atoi,atof function.
1, Atoi, ASCII to Integer, converts the string to int
2, Atof, ASCII to float, convert string to double
3, ATOL, ASCII to long int, converts the string to a long int
4, atoll, ASCII to long long int, converts the string to a long long int
For example the above input of 1996, can be obtained as follows:
int year = atoi (argv[2]); Year = 1996
Therefore, through the command line input above and the type conversion inside the program, you can pass in the Command line window to pass in the value (string and number) to run in the program.
Parameters for C + + main function