Hello, I am interested in the main function with parameters, I often see an entry in other people's sources, and in my own (I am writing this part unconsciously)

int main(int argc, char **argv) 

How are these parameters passed to the function when it is called and what they mean in general, thanks.

    5 answers 5

    These are the parameters that are passed to the program at startup.

    argc is the number of these parameters. argv is a pointer to an array of pointers to strings, each of which contains one parameter. Parameters are passed as C strings. There is another such record - char * argv []

    In the memory structure of the Linux process, they are on the stack right behind the stack of the main function. I think in other systems the same device.

      You were shown here with examples. I forgot the truth about the third parameter main () a reference to the vector of the OS environment variables (environment, which are set in the Shell by the export command)

      Generally fully:

       int main (int ac, char **argv, char **env) 

      Read also man execl.

      • one
        Too lazy to look for c ++ descriptions, and in ISO / IEC 9899: 1999 (E) (this is about C99), the number of main arguments is 0 or 2. Section 5.1.2.2.1 Program startup - alexlz
      • N3291 3.6.1.1: int main () {/ * ... /} and int main (int argc, char argv []) {/ * ... * /} - gkuznets
      • Obviously, in practice, it is possible to define 0,1,2 or 3 parameters in main (). I do not know how it is correct for your documentation, but the program works. #include <stdio.h> #include <stdlib.h> main (int ac, char * av [], char * ev []) {int i; for (i = 0; ev [i]; i ++) printf ("% s \ n", ev [i]); printf ("environment% d items \ n", i); exit (0); } and they always worked in both * nix and Windows. I am sure that VAX / VMS also works. I don't know about forgotten exotic, like RT-11. - avp
      • 2
        Wikipedia en.wikipedia.org/wiki/Main_function states that unix (but not POSIX.1) and MS Windows. - alexlz
      • Well, that sorted out! Good link. Did not know about the fourth argement in apple. Thank ! - avp

      This is a standard C / C ++ thing.

      • argc - the number of arguments in the command line
      • * argv [] - an array of pointers to the lines containing these arguments

      In this case, argv [0] is the name of the command, and argv [argc] is NULL.

        baz.cpp:

         #include <iostream> int main(int argc, char** argv) { std::cout << "argc: " << argc << "\n"; for (int i =0; i < argc; ++i) std::cout << i << ": " << argv[i] << "\n"; } 

        We look:

         $ g++ baz.cpp -o boo $ ./boo foo argc: 2 0: ./boo 1: foo 
        • And what do these parameters mean? - username76
        • [Nothing significant (:] [1] [1]: en.wikipedia.org/wiki/… - gkuznets
        • And you can output them either: copy (argv, argv + argc, ostream_iterator <char *> (cout, "\ n")); - skegg
        • while(argc--) cout << *argv++ << endl; - alexlz
        • You can too. - skegg

        If your program is called myprog , then let's say you call it with parameters like this:

        myprog param1, param2, param3

        Accordingly, in main() you will get:

         argc=3 //количество параметров argv[0]="param1"; //1-й параметр argv[1]="param2" //2-й параметр argv[3]="param3" //3-й параметр 

        Moreover, if at the end of the program you return to main() return 2; then the axis will receive an exit code equal to 2 (in Windows, this is called errorlevel )

        Update Sporol I get on with an example - I correct:

        myprog param1 param2 param3

        Accordingly, in main() you will get:

         argc=4 //количество параметров argv[0]="myprog"; //название запускаемого файла argv[1]="param1" //1-й параметр argv[2]="param2" //2-й параметр argv[3]="param3" //3-й параметр 
        • wrong example - gkuznets
        • one
          This is correct for Java. In C / C ++, by convention, argv [0] is the name of the program. - skegg
        • one
          And who ate commas? @mikillskegg For jaba, speak? And where did argc come from? Probably there somewhere argc=argv.lenght in the bushes ... - alexlz
        • Yes lazhanulsya - it happens. I haven’t taken C / C ++ for a long time and messed up with commas :) Schaz corrected - Barmaley
        • Although, in fact, launching the process can insert anything into argv [0]))) - skegg