argc and argv

argv and argc

Since a C program is executed as if it is a function called by the OS, the OS can and does pass parameters to the program.

There are two parameters. These two parameters fully specify the command line that was used to invoke the executing program. The first parameter is argc. It is an integer that represents the number of white space separated strings on the command line. In this case, white space refers to space and tab characters.

If a program is executed (invoked) with a command line that looks like

% a.out

then the value of argc is 1 (one). Note that the percent sign (%) represents the Unix prompt. The prompt is not part of the command line.

If a program is executed (invoked) with a command line that looks like

% my.simulator -runs 12 -max 100 -file x.input

then the value of argc is 7 (seven).

The second parameter is argv. It is an array of pointers to characters. Found at the other end of the pointer (dereferencing the pointer) is not just a character, but a null-terminated string. Each of these strings is one of the white space separated command line arguments.

If a program is executed (invoked) with a command line that looks like

% prog 6 -why file.c

then the value of argc is 4 (four), and argv can be diagrammed as

  argv
  ---         ---        -------------------
 |  -|---->  |  -|----> | p | r | o | g |\0 |
  ---        |---|       -------------------
             |  -|----> | 6 |\0 | 
             |---|       -------------------
             |  -|----> | - | w | h | y |\0 |
             |---|       ---------------------------
             |  -|----> | f | i | l | e | . | c |\0 |
             |---|       ---------------------------

Each of the values within the boxes to the right (representing each string) is a single character. Single quote marks are often used to indicate characters, but they have been omitted from this diagram to reduce clutter. The character '\0' is a standard way (in C code) to denote the NULL character.

argv[0] is the (pointer to the) string "prog".
argv[1] is the (pointer to the) string "6".
argv[2] is the (pointer to the) string "-why".
argv[3] is the (pointer to the) string "file.c".

In addition, for ANSI standard C, the array element argv[argc] exists, and is defined to be a null pointer. Therefore, the diagram for the example command line given will be:

  argv
  ---         ---        -------------------
 |  -|---->  |  -|----> | p | r | o | g |\0 |
  ---        |---|       -------------------
             |  -|----> | 6 |\0 | 
             |---|       -------------------
             |  -|----> | - | w | h | y |\0 |
             |---|       ---------------------------
             |  -|----> | f | i | l | e | . | c |\0 |
             |---|       ---------------------------
             | 0 |
             |---|

A lazy (and incorrect) terminology for argv calls it an array of strings. This is poor terminology, because while argvis an array, each element of the array is a pointer to a character. To further muddy the waters, the C programming language does not distinguish between a pointer to a character and a pointer to a string. In fact, there is no type that is a pointer to a string, because a string is not a primitive or predefined type. A pointer to a character is a well defined type. The C programming language does not care whether the pointer identifies a single character or the first of a consecutive set of characters (where the last character happens to be the NULL character).

The main function that declares argv and argc as arguments appears as

 int main (int argc, char *argv[]){ }

When using the command line arguments, always check that an argument exists by using argc before attempting to access the argument.

Important Items to Understand

  • The integer parameter argcincludes the name of the program invoked as one of the count. The command line

      % ls -l
    has 2 command line arguments. argc = 2. The first element of argv gives us a pointer to the string "ls", and the second element of argv gives us a pointer to the string "-l".

     

  • The array that is argv has elements that are pointers to characters. A string is not a type in the C programming language. In the C programming language, there is no syntax distinction between a pointer to a character, and a pointer to a string.

posted @ 2013-11-12 17:35  永久指针  阅读(307)  评论(0编辑  收藏  举报