SoC Logo HowTo
Use Command Line Arguments in C Prof. Bartenstein
 

Contents:

What are Command Line Arguments?

When you invoke a command you typically type the name of the command, followed by some extra information. The command name and the extra information is collectively called the command line arguments. These arguments are available from inside the program, and the program may use them to control how the program runs.

arrow_circle_up

UNIX Command Line Processing

UNIX command lines from a terminal are processed by a shell program. There are many different shell programs, and shell programs often have slightly different command line processing syntax and semantics. This page describes the most common syntax and semantics for command line processing. You can find out which shell you are running by typing the command echo $SHELL.

In general, shells look at the first blank delimeted word of a command to determine which command to execute. If the first word does not have a path name, then the shell will often use the PATH environment to find the command. The remaining words, up to the first redirection token, are treated as command line arguments, and passed in to the program to process. The command line arguments may be followed by a redirection token; a word that starts with the character, '<', '>', or '|', or a number followed by '<' or '>'. Redirection tokens (and anything after) are consumed by the shell and used to manage standard I/O connections. Redirection tokens are not passed to the program as command line arguments.

The shell typically parses the command line based on white space (one or more blanks, tabs, or even newlines). The white space itself is discarded, but each white-space delimited token is treated as a separate command line argument. It is possible to create arguments that contain white space by quoting an argument string. Each quoted argument string becomes a separate command line argument, even if it contains white space. Different shells have different quoting conventions, but almost all support double quotes (") as beginning and ending quote delimiters.

arrow_circle_up

Reading Command Line Arguments in C

In the C language, command line arguments are read using parameters to the C main function. C programs that don't use command line arguments can use the simplified main function declaration: int main() { ... . In order to access the command line arguments, a more complicated main function declaration is required:


int main(int argc,char **argv) { .... 

The argc parameter contains the number of command line arguments found on this command invocation, and the argv parameter contains a list of pointers to each command line argument.

Note: you don't have to use the names argc and argv, but everybody uses these names, and it's much easier to stick with this convention.

arrow_circle_up

Using argc

The argc parameter contains the number of command line arguments found in this invocation of the command. Note that since the command name itself is also a command line argument, there is always at least one command line argument! Therefore, if you are expecting a single parameter other than the command name, argc should have a value of 2.

Programmers often use argc to check to make sure the correct (or minimum) number of command line arguments are specified. A typical program which makes this check might be something like:


int main(int argc,char **argv) {
	if (argc < 3) {
		printf("The %s command requires at least two command line arguments, x and y\n",argv[0]);
		return 1;
	}
	...

Note that this code uses argv[0] to reference the command name specified by the user to invoke this command. Also, the main function returns a non-zero return code (1 in this example) to indicate that the program did not run correctly.

arrow_circle_up

Understanding argv

The traditional declaration of argv is char **argv. In C, this means that argv is a pointer to a pointer to a character. (How confusing!) This becomes a little clearer if we remember that a string of characters in C is just an array of characters, and in the C language, a pointer can be thought of as an array of unspecified length. Thus, char **argv can be thought of as char *argv[]. If there were a string data type in C, we could think of this as string *argv. Again, since a pointer is an array of unspecified length, think of this as string argv[]. Really, argv is just an array of strings. Since the argv "array" is of unspecified length, we need the argc parameter to tell us how many elements are in the argv array.

The C language allows us to switch back and forth between array and string notation. That means we can reference the command line parametes using C notation like argv[0] to get the string of the command itself, argv[1] to get at the next token after the command name, argv[2] to get the next token, and so on.

Remember, each of the command line arguments are ASCII character strings. It is not valid to use an ASCII character string directly as a number.

Using argv

The argv array of strings can be used in the C language directly as a string. For example, to check for equality, we might use something like:


if (0==strcmp(argv[1],"verbose")) {
	// Turn on verbose mode
	...

It is also possible to look at a string a single character at a time. For example, we might want to do something like:


for(int i=1;i <argc; i++) {
	if (argv[i][0]=='-') {
		// Argument i starts with a dash.. it's a flag
	...

In order to treat a command line argument as a number, we need to translate the argument from a character string into a number. Here is an example that does that:


int num=atoi(argv[2]); // Convert the third command line token to a number

arrow_circle_up

Command Line Argument References

arrow_circle_up