This question has already been answered:

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

What is the meaning of the specified function arguments?

Reported as a duplicate by the participants vp_arth , αλεχολυτ , Ruslan_K , Regent , Harry c ++ Feb 23 '17 at 4:13 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • hmm ... what's the point of passing parameters to the program? - pavel
  • 7
    I wonder if the question of the type “why the main function is needed” will appear soon. - αλεχολυτ

2 answers 2

The general meaning of the arguments is to set the program arguments via the command line when the program is started.

In this ad

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

the argc parameter specifies the total number of arguments passed to the program when it is started, and the argv array specifies pointers to string representations of the arguments. If argc not equal to 0, then argv[0] specifies either the name of the program being run, or, if the environment does not pass the name of the program to the program, then the empty string "" . The value of argv[argc] always 0.

You can use any names instead of argc and argv at your discretion.

You can print the arguments passed to your program in various ways. For example,

 #include <iostream> int main( int argc, char * argv[] ) { for ( char **p = argv; *p; ++p ) std::cout << *p << std::endl; } 

If your program does not need any arguments passed to it at startup, then you can declare the main function as

 int main() 

    argc is the number of elements in the argv array.

    argv is an array of parameter strings passed on the command line.

     prog.exe first second third agrv[0] - имя файла prog.exe argv[1] - first argv[2] - second argv[3] - third 

    Some operating systems (and the programs themselves) can process the parameters — for example, by combining what is in quotes into one parameter, or replacing a pattern of type * with a list of file names that matches the pattern, but that is another question ...

    • one
      argv [0] does not always contain the name of the program in Windows, if you rely on it - it can be a lot of fun ... - Vladimir Martyanov
    • one
      @ Vladimir Martianov By the way, really - where and when is this observed? Although yes, this is written and warned - so far I have never really come across it. Well, unless it is specifically in spawn... to transmit some sort of nonsense, and that is just nonsense, and not a null pointer - otherwise spawn... frayed (VC ++, the same Open Watcom will still pass the correct name not to enter as argv[0] ). But, since you wrote - “far from always” - then where is this found in the real, so to speak, world, and not in the code specially created for this? - Harry
    • one
      @Harry: Easy, run the program with CreateProcess and transfer any nonsense to lpCommandLine . - VladD
    • @Harry: And in the real world, this occurs if your code is executed in a hostile environment, and you rely on the argv[0] value for all sorts of security related things. For example, by program name, look for the name of the file with the settings. - VladD
    • @VladD Ie in any case, this is done deliberately, not because this is done in this OS / compiler / etc ... QED - Harry