Arguments within main(int argc, char *argv[])
Posted by Samath
Last Updated: October 31, 2012
A lot of new programmer wonder what are the arguments passed to main. I am going to tell you about them.

The program has two argument in main as in

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

basically, argc is an integer that contains the number of arguments passed to the program and argv is an array of the arguments that was passed.

argv[0] is always the program name itself.



#include <iomanip>
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
  {
  cout << "The name used to start the program: " << argv[ 0 ]
       << "\nArguments are:\n";
  for (int n = 1; n < argc; n++)
    cout << setw( 2 ) << n << ": " << argv[ n ] << '\n';
  return 0;
  }

If you compile this from the command line and enter:

D:\prog\test> a Hello world!


Output:

The name used to start the program: a

Arguments are:

1: Hello

2: world!