Command-line arguments are a common way to parameterize execution of programs. For example, gcc hello.c -o hello uses hello.c and -o hello as arguments to the gcc program.
To experiment with command-line arguments, it’s best to compile the program first:
In C, command-line arguments are passed to the main function through the parameters int argc (argument count) and char *argv[] (argument vector). The argc parameter contains the number of arguments, and argv is an array of strings containing the arguments.
The first element of argv (index 0) is always the name of the program itself. The actual command-line arguments start from index 1.
Note that in C, we need to manually iterate through the argv array to print all arguments, unlike the automatic printing in the original example.
Next, we’ll look at more advanced command-line processing with option parsing libraries like getopt.