Command Line Arguments

main() revisited

  1. The main function actually takes two arguments - argc and *argv[]
  2. argc is an integter with the value equal to the number of arguments passed
  3. argv is a pointer array that points to each argument passed
Example:
int main(int argc, char * argv[])
{

    if (argc == 2)
        printf("%s was passed", argv[1]);

    return 0;
}

*argv[0]

  1. *argv[0] is a pointer to a string representing how the program was invoked
  2. So, if no arguments were passed, argc would equal 1
  3. For n arguments, argc = n + 1