In the Main function of the argument argc argv use details

  • 2020-04-01 23:45:08
  • OfStack

C/C++ language in the main function, often with arguments argc, argv, as follows:

int main(int argc, char** argv)

What do these two parameters do? Argc is the number of arguments entered on the command line. Argv stores all the arguments. If your program is hello.exe, if you run the program from the command line (you should first enter the directory where the hello.exe file is located with the CD command from the command line), run the command as follows:

hello.exe Shiqi Yu  

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/201305241057496.gif ">

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;
}

If the above code is compiled as hello.exe, 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.

Related articles: