User Tools

Site Tools


haas:fall2020:common:projects:commandlineargs

This is an old revision of the document!


Command-Line Arguments

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/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.

argument naming conventions

Although you can name them anything, conventionally in many documents these have been named:

  • argc (or ac): the argument count, a signed integer (int argc)
  • argv (or av): the array of strings (or an array of an array of char), a double pointer (char **argv)

accepting command-line arguments in your program

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).

header files

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>

setting up main()

To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional argc and argv names, although it is also common to see them abbreviated as ac and av.

Please declare your main() function as follows:

int main(int argc, char **argv)

There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like “ac” and “av”):

  • int argc: the count (an integer) of tokens given on the command line (program name + arguments)
  • char **argv: an array of strings (technically an array of an array of char) that contains “strings” of the various tokens provided on the command-line.

The arguments are accessible via the argv array, in the order they were specified:

  • argv[0]: program invocation (path + program name)
  • argv[1]: our maximum / upper bound
  • argv[2]: reserved value, should still be provided and be a 1 for this project
  • argv[3]: conditionally optional; represents lower bound
  • argv[4]: conditionally optional; represents upper bound

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.

example

For example, if we were to execute the primereg program:

lab46:~/src/discrete/pnc0$ ./primereg 128 1 2 2048

We'd have:

  • argv[0]: “./primereg”
  • argv[1]: “128” (note, NOT the scalar integer 128, but a string)
  • argv[2]: “1”
  • argv[3]: “2”
  • argv[4]: “2048”

and let's not forget:

  • argc: 5 (there are 5 things, argv indexes 0, 1, 2, 3, and 4)

With the conditionally optional arguments as part of the program spec, for a valid execution of the program, argc could be a value anywhere from 3 to 5.

Simple argument checks

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 + quantity + argv[2] == 3) have been provided
    {
        fprintf(stderr, "%s: insufficient number of arguments!\n", argv[0]);
        exit(1);
    }

Since argv[3] (lower bound) and argv[4] (upper bound) are conditionally optional, it wouldn't make sense to check for them in the overall count. But we can and do still want to stategically utilize argc to determine if an argv[3] or argv[4] is present.

Grab and convert max

Finally, we need to put the argument representing the maximum quantity into a variable.

I'd recommend declaring a variable of type int.

We will use the atoi(3) function to quickly convert the command-line arguments into int values:

    max  = atoi (argv[1]);

And now we can proceed with the rest of our prime implementation.

haas/fall2020/common/projects/commandlineargs.1602765656.txt.gz · Last modified: 2020/10/15 12:40 by wedge