Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) argv

 

argv holds the first index of an array of strings, where argc holds the size of argv. With argc and argv you can access the arguments main is called with from (by the operating system).

 

One of the two standard forms of main is [1]:

 

int main() { /* Your code */ }

 

argv contains the filename of the program itself at index zero and then the parameters the user gave when starting the executable.

 

 

 

 

 

Example

 

This example shows all parameters a user entered.

 

#include <iostream>

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

 

This means if you start the program (from command-line) with for example the following line:

 

TestMain Hello World

 

Your output will be something like:

 

0 : TestMain
1 : Hello
2 : World

 

 

 

 

 

References

 

  1. C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 3.6.1.2

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict