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

Usually we see the main functions are no arguments, in fact, the main function can be parameterized. The main function of the parameters of who came of it? The answer is that the operating system, C ++ predetermined parameter can only have two main functions, i.e., the argv and argc, shown with the main function in the form of the following parameters.

int main(int argc,char*argv[])
{
    ……
}

The first argument must be an integer variable argc, called counter parameter whose value is the number of parameters, including the command name;

The second argument must be argv array of character pointers, store name and address command parameter string.

To invoke the parameterized function must be performed at the main operating system environment, see the following sample code:

#include <the iostream>
 the using  namespace STD;
 int main ( int argc, char * the argv []) // two parameters 
{
     for ( int I = argc- . 1 ; I> = 0 ; i--) // backwards Output , 0 to argc-1, to prevent cross-border     
    {     
        COUT << the argv [I] << endl;     
    }     
    return  0 ; 
}

The code compiled and linked to produce an executable file, assuming it is named example.exe, copied to the C root directory, enter the following command line in a DOS environment:

example world hello

 Example a file name itself can be considered a parameter, so that the common command line call example.exe three parameters, the parameters of the DOS system functions argc main transmission 3, character pointer argv array elements in argv [0], argv [1 ], argv [2] are stored strings example, world, hello address, according to the definition of the main function, the three reverse output string.

Guess you like

Origin www.cnblogs.com/cyx-b/p/12513229.html