Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

argv

C uses a mechanism which you will find reminiscent of java's command-line-argument mechanism (the "String []args" argument to main()). There is an alternate allowable definition of main() in C and C++, whose declaration looks like this:

        int main(int argc, char **argv)

"argc" stands for "argument count". It is the number of elements in "argv", "argument vector". (While "vector" and "array" mean different things in java's datatype library, in general they are synonyms.)

While "argc" and "argv" are theoretically arbitrary parameter names, the use of the names "argc" and "argv" is so standard that you should never use any other name.

Since a string can be passed around as a value of type pointer-to-char, argv will be an array of pointers-to-char. And when argv is passed to main() by the operating system, it will decay into a pointer to its zeroth element, so the data will be of type pointer-to-pointer-to-char.

The array is one element larger than you might think. The program name itself is argv[0], so the first command-line argument is argv[1], and so on, and argc reflects the total number of items in the argv array including this argv[0]. Often we ignore argv[0] altogether.

Example 1: Write a program which first checks that argc is at least 2, then prints the value of argv[1] (using %s). A session with this program might look like this, where '$' is your shell prompt:

	$ ./a.out hello, world
	hello,
	$ 
Since "world" is argv[2], it didn't print that part. The shell (command interpreter) divides up the arguments based on spaces.

Solution:

#include <stdio.h>

int main(int argc, char **argv)
{
    if (argc >= 2)
	printf("%s\n", argv[1]);
    return(0);
}
We check that argc >= 2 before accessing argv[1], because it is illegal to exceed the bounds of an array in C, and it is undiagnosed... be careful!

Example 2: Write a program which prints all of the argv values in a loop. This is like the 'echo' command in unix.

Sample session (our program is not going to be quite as tidy as 'echo', as shown here):

	$ echo hello, world, how are you
	hello, world, how are you
	$ ./a.out hello, world, how are you
	hello,
	world,
	how
	are
	you
	$ 

Answer:

#include <stdio.h>

int main(int argc, char **argv)
{
    int i;
    for (i = 1; i < argc; i++)
	printf("%s\n", argv[i]);
    return(0);
}

Note the ignoring of argv[0], which would be "./a.out".

Example 3: Finally, here is a more traditionally-written unix "echo" command (although not implementing the "−n" option processing). Understanding this code requires understanding the C pointers material (not just arrays), and also the C comma operator.

#include <stdio.h>

int main(int argc, char **argv)
{
    for (argc--, argv++; argc > 0; argc--, argv++)
	printf("%s%c", *argv, (argc == 1) ? '\n' : ' ');
    return(0);
}

I suggest spending the necessary time to understand how all of that works (possibly by asking me about it). (But only after learning the C pointers material.)