To facilitate our activities, we will be making use of command-line arguments in our programs.
Command-line arguments are simply the tokens (space-separated pieces of information) we provide to the computer at the prompt:
lab46:~/src/SEMESTER/DESIG/project$ ./project token1 token2 ... tokenN
The operating system packages up this information and provides it to our program, allowing us to access it.
The first token (“./project”) will be the invocation of our program, allowing our programs to know what they are called.
The second token is the argument that immediately follows (in our case, “token1”); the third is “token2”, and on and on until our very last provided argument (“tokenN” in our conceptual example).
In addition to all the arguments packaged together in an array of strings, we are also provided with a total overall argument count, so we can know how many elements there are in this array of strings.
Although you can name them anything, conventionally in many documents these have been named:
To access this command-line information, we provide arguments to our main() function, as follows:
int main (int argc, char **argv) {
We then have access to those populated variables (when we run a program, we are calling main(), so it should make sense that the arguments we provide to our program are passed as parameters to our main() function, the starting point of our program).
The arguments are accessible via the argv array, in the order they were specified:
N in this case is equivalent to (argc - 1), as is commonly the case with array size and addressing.
Additionally, let's not forget the argc variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5 (because array elements 0-4 indicate 5 distinct array elements).
For example, if we had the following program:
lab46:~/src/SEMESTER/DESIG/project$ ./project 128 1 2 2048
We'd have:
and let's not forget:
While there are a number of checks we should perform, one of the first should be a check to see if the minimal number of arguments has been provided:
if (argc < 3) // if less than 3 arguments (program_name + argv[1] + argv[2] == 3) have been provided { fprintf(stderr, "%s: insufficient number of arguments!\n", argv[0]); exit(1); }
Trying to access a non-existent argument could result in a segmentation fault. ALWAYS check your count to ensure the desired argument exists at the given position.
We don't need any extra header files to use command-line arguments, but we will need an additional header file to use the atoi(3) function, which we'll use to quickly turn the command-line parameter into an integer, if such an operation is needed; and that header file is stdlib.h, so be sure to include it with the others:
#include <stdio.h> #include <stdlib.h>
In the above example, if argv[1] was “128” and we actually wanted that as an integer (versus the string it is initially available as), we utilize atoi(3) in the following manner:
int value = 0; value = atoi (argv[1]);
NOTE: you will want to do proper error checking and first ensure that argv[1] even exists (by checking argc). Failure to do so may result in a segmentation fault.