Running the program and the result:


How it looks like in the memory:


Some comments and key points about argv and argc:

  • Both *argv[] and **argv are considered equivalent and both forms can be seen in the wild depending on what the programmer prefers. This is because in the memory both are the same.
  • If you don’t plan to accept any arguments in your program, it’s recommended to use the format we’ve used so far – int main(void)
  • argc (argument count) is the number of arguments passed to the program when it was run. It is also the length for the first dimension of argv.
  • argc is always at least 1, but it can be greater. It is 1 if no arguments were passed.
  • argv (argument vector) contains all of the command line arguments passed to the program when it was started
  • argv[0] contains the name of the program that was started
  • argv[i] points to the beginning of the i-th string that was passed to the program. E.g. argv[2]will contain “-test”
  • Everything passed to the program is considered to be string (not automatically converted). This means that the length for each of the strings can be found using strlen() function.

An example of something¬† we’ve used before:

To compile a program from the command line we ran something like:

gcc -o mysupersecretprogram -Wall -Wconversion sourcecode_file.c

In this case the number of arguments would 6.

  • First one (argv[0]) would be the name of our compilator gcc.
  • -o mysupersecretprogram – 2 arguments but they must be placed next to eachother. -o indicates that we want to specify the name of the output binary file and the name following it will be the name we give it.
  • -Wall – an argument that makes gcc output most of the important warnings that might cause our program issues
  • -Wconversion – an argument to gcc that makes it display typecasting issues
  • sourcecode_file.c – the last argument, it will say which codefile to compile.