The main function in C/C ++ often carries the argc and argv parameters, as follows:
Int main (INT argc, char ** argv)
What are the functions of these two parameters? Argc refers to the number of input parameters in the command line. argv stores all command line parameters. Assume that yourProgramIs hello.exe. If you run this program on the command line (first, use the CD command to enter the directory where the hello.exe file is located), run the following command:
Hello.exe Shiqi Yu
The value of argc is 3, argv [0] is "hello.exe", argv [1] is "Shiqi", and argv [2] is "yu ".
The following program demonstrates the use of argc and argv:
# 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 ;}
Assume thatCodeCompile as hello.exe, then run:
Hello.exe a B c d e
Will get
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:
Hello.exe lena.jpg
Will get
Argument 0 is hello.exe. Argument 1 is lena.jpg.