The main function in C + + language, often with parameter argc,argv, is as follows:
Copy Code code as follows:
int main (int argc, char** argv)
What are the roles of these two parameters? ARGC refers to the number of command line input parameters, argv stores all command-line arguments. If your program is hello.exe, if you run the program at the command line (you should first enter the directory of the Hello.exe file with a CD command at the command line), run the command:
Copy Code code as follows:
The following program demonstrates the use of argc and argv:
Copy Code code as follows:
#include <stdio.h>
int main (int argc, char * * argv)
{
int i;
for (i=0 i < argc; i++)
printf ("Argument%d is%s.\n", I, argv[i]);
return 0;
}
If the above code is compiled to Hello.exe, then run:
Copy Code code as follows:
Will get
Copy Code code as follows:
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.
Run:
Copy Code code as follows:
Will get
Copy Code code as follows:
Argument 0 is hello.exe.
Argument 1 is lena.jpg.