C language parameters argc and argv main function

  References:

  http://wiki.opencv.org.cn/index.php/Main%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0argc%EF%BC%8Cargv%E8%AF%B4%E6%98%8E

C ++ language main function, often with parameters argc, argv, as follows:

int main(int argc, char** argv)
int main(int argc, char* argv[])

Effect of these two parameters: argc is the number of command line arguments (in whitespace separated) stores all the argv command line parameters are hello.exe if your program, if the program is run from the command line, (first should enter at the command line using the cd command to hello.exe file directory) command is run:

hello.exe Shiqi Yu

Then, the value of argc is 3, argv [0] is "hello.exe", argv [1] is "Shiqi", argv [2] is "Yu". Image:Hello-argc-argv.png

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 hello.exe, then run

hello.exe a b c d e

Will be

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 be

Argument 0 is hello.exe.
Argument 1 is lena.jpg.

Guess you like

Origin www.cnblogs.com/chester-cs/p/11915451.html