Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 1.28 KB

CppArgc.md

File metadata and controls

57 lines (42 loc) · 1.28 KB

argc ('Argument Count') holds the size of array argv, where argv holds the first index of this array of strings. 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(int argc, char * argv[]) 
{ 
  // Your code here
}

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

Example: shows all command-line arguments

This example shows all command-line arguments:

#include <iostream>

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

If you start the program from the command-line as such:

my_program_name Hello World

Your output will be similar to:

0 : /home/richelbilderbeek/my_program_name
1 : Hello
2 : world

The first argument, argv[0] is always the path to the program.  

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