Accessing command line arguments

Sample execution of a program using command line arguments

Some notable points

  • This form of main function ( int main(int argc, char *argv[]) ) should only be used when you intend to accept command line arguments.
  • It doesn’t matter which form of argument vector you are using. Both   *argv[]  and **argv  are equivalent.
  • argc  will tell you how many arguments were passed to your program. This is also the length of the argument vector.
  • argc  value will be 1, if no arguments were passed to the program.
  • argv  is the argument vector that contains all passed arguments as strings. It reality it’s an array of pointers to strings. Each pointer will be pointing at one of the arguments (strings).
  • argv[0]  contains the name of the executable (and how it was executed)
  • argv[i]  contains the i-th argument as a string. I.e.   argv[1]  contains the first argument.
  • All argumetns are passed as strings, even if they only contain numbers.