CS计算机代考程序代写 Command Line Arguments and Makefiles in C

Command Line Arguments and Makefiles in C

Command Line Arguments in C
CSE 2421

Recommended Reading: Pointers On C, Chapter 13, Sections 13.4 through 13.4.2

How arguments are passed to main()
We know that, when we call a function in a C program, arguments can be passed to the function.

When we execute a C program from the command line, we can also pass arguments to main().

Let’s see how this is done.

Passing arguments from the command line
main() is invoked when we run a command on the command line to execute a C program. For example, for a program called myProg:
% myProg

If we want to pass arguments, or parameters, say p1, p2, and p3, to main() in myProg, we enter them on the command line after the name of the program:
% myProg p1 p2 p3

then, we’ll declare main with parameters so we can access them

Options for main() with arguments
Any arguments passed from the command line will be placed in read-only memory.
Remember string-literals are put there, too
If we want to pass arguments to main() from the command line, we have two appropriate declarations of the parameters for main() inside the program:
int main(int argc, char **argv) { … }
int main(int argc, char *argv[]) { … }

One of these approaches uses a pointer to a pointer to char for the 2nd parameter and the other uses an array of char pointers.

These two declarations are equivalent (they are just different ways of looking at the same thing – more explanation below).

Parameters on the command line
The parameters given on the command line are passed to a C program using two variables, which have the following names, by convention:
1. argc contains the count of the command line arguments. Think “argcount” or “argument count”
2. argv contains pointers to the individual arguments as character strings (argv[] is an array of char *). Think “argvector” or “argument vector”.

In the declaration of main(), the names of argc and argv may be any valid identifiers in C, but it is a common convention to use these names.

NOTE!!: There is no guarantee that the strings pointed to by the pointers in argv are stored in contiguous locations in memory, as they would be in a normal array, because argv is an array of pointers to these strings!

Example
Again, suppose that, at the command line prompt, we enter:
% myProg p1 p2 p3

This results in argc containing the value 4 and the following strings to which the elements of argv point, because argv[] is an array of char * (or a char **):

argv[0] points to “myProg\0” /*string with program name */
argv[1] points to “p1\0”
argv[2] points to “p2\0”
argv[3] points to “p3\0”
argv[4] is a NULL pointer

Note that, because these strings are of type char *, they are stored in read only memory, and any attempt to write to them will result in a segmentation fault!

Also note that each argument is an ASCII string terminated by a NULL byte (‘\0’).

A more detailed view
Consider:
argv-> 0x800540->”myProg”

0x8012A0->”p1”

0x800260->”p2”

0x80200C->”p3”
argc = 4

Note: each individual string is in continuous memory locations, but each string can be in various locations
0x800540
0x8012A0
0x800260
0x80200C
0x000000
540 541 542 543 544 545 546
m y P r o g \0

2A0 2A1 2A2
p 1 \0

260 261 262
p 2 \0

00C 00D 00E
p 3 \0

More on argv
Recall that any array may be viewed as a (constant) pointer to the first element of the array.

For this reason, argv can be declared in one of two ways:
int main(int argc, char *argv[]);
int main(int argc, char **argv);

Thus, argv may be viewed as an array of char *, or as a char ** (a pointer to a pointer to char).

Which way you choose to declare it in your programs is up to you. Pick the way that is easier for you to visualize your data.

int main(int argc, char *argv[])
After the arguments are passed to main(), we can access them, just as any other function parameters:

#include
int main(int argc, char *argv[]) {
int i;
printf(“argc =\t%d\n”, argc);
for (i = 0; i < argc; i++) printf(“argv[%i]\t= %s\n”, i, argv[i]); return (0); } OR int main(int argc, char **argv) { char **ip = argv; int i=0; printf(“argc =\t%d\n”, argc); while(*ip!= NULL) printf(“argv[%i]\t= %s\n”, i, *ip++); return (0); } Notice first version uses argc to loop, the second uses the fact that argv[argc]==NULL What is printed? For the command line input: “myProg p1 p2 p3” The programs on the previous slide both print: argc = 4 argv[0] = myProg argv[1] = p1 argv[2] = p2 argv[3] = p3 How can we use these parameters? The name of the program, argv[0], can be useful when printing diagnostic or error messages. Usually, multiple processes (or programs) are running on the system, so it is useful to be able to identify which process caused an error. printf(“myProg expects 3 parameters, only %d were entered\n”, argc-1); or a better way… printf(“%s expect 3 parameters, only %d were entered\n”, argv[0], argc-1); The second option allows for the myProg executable to be copied to another filename. (e.g. cp myProg sumProg). If we use the 2nd option when using the program with it’s new name (sumProg), the error message is still correct. The values of the other parameters can be accessed and used just as any other function parameters. More on argc and argv It is guaranteed that argc is non-negative, and that argv[argc] is a NULL pointer. By convention, the command line arguments specified by argc and argv include the program name as the string that is pointed to by argv[0]. For example, if a user types the command “rm file”, the shell will initialize the rm process with argc = 2, argv[0] as “rm”, and argv[1] as “file”, and argv[2] as 0 [the value of a NULL pointer]. Remember that the main() function is special. Every C program must define it exactly once. Command Line Arguments - Example /* sum.c ** This program sums the integer values that correspond to the ** strings from argv[1] to argv[argc – 1] */ #include
int main(int argc, char *argv[]){
int i;
int sum = 0;
for (i = 1; i < argc; i++) sum = sum + ( atoi (argv[i]) ); /* atoi converts a string to an int */ printf(“The sum of the command line arguments after the first is %i.\n”, sum); return (0); } Why do we need to use the atoi() function??? Command Line Arguments - Example /* sum.c ** This program sums the integer values that correspond to the ** strings from argv[1] to argv[argc – 1] */ #include
int main(int argc, char *argv[]){
int i;
int sum = 0;
for (i = 1; i < argc; i++) sum = sum + ( atoi (argv[i]) ); /* atoi converts a string to an int */ printf(“The sum of the command line arguments after the first is %i.\n”, sum); return (0); } Why do we need to use the atoi() function??? ALL parameters are ASCII strings!! Command Line Arguments – Example Now, after compiling the program, suppose we enter on the command line: % sum 100 50 25 The program prints out: The sum of the command line arguments after the first is 175.