ch2-arglist
Chapter_2 | getopt |
arglist.c ALP, p. 28
#include <stdio.h> // for printf(), putchar()
int main (int argc, char* argv[])
{
printf ("The name of this program is \"%s\"\n", argv[0]);
printf ("This program was invoked with %d argument", argc - 1);
if (argc != 2) {printf("s\n");}
else {putchar('\n');}
// Were any command-line arguments specified?
if (argc > 1) // Yes, print them
{
int i;
if (argc != 2) {printf ("The arguments are:");}
else {printf ("The argument is:");}
for (i = 1; i < argc - 1; i++) // skip program name, argv[0]
{printf (" %s", argv[i]);}
printf (" %s\n", argv[i]);
}
return 0;
}
/*
gcc arglist.c -o arglist
./arglist
The name of this program is "./arglist"
This program was invoked with 0 arguments
./arglist Hello
The name of this program is "./arglist"
This program was invoked with 1 argument
The argument is: Hello
./arglist Hello, world!
The name of this program is "./arglist"
This program was invoked with 2 arguments
The arguments are: Hello, world!
*/
Chapter_2 | BACK_TO_TOP | getopt |
Comments
Post a Comment