Command Line Parameters in C Programming Language

Many programs allow command-line arguments to be specified when they are run. A command-line argument is the information that follows the program's name on the command line of the operating system. Command-line arguments are used to pass information to the program. For example, when you use a text editor, you probably specify the name of the file you want to edit after the name of the word processing program. For example, if you use a word processor called WP, then this line causes the file TEST to be edited.

WP TEST

Here, TEST is a command-line argument. Your C programs may also utilize command-line arguments. These are passed to a C program through two arguments to the main() function. The parameters are called argc and argv. These parameters are optional and are not used when no command-line arguments are being used.

The argc parameter holds the number of arguments on the command-line and is an integer. It will always be at least 1 because the name of the program qualifies as the first argument. The argv parameter is an array of string pointers. The most common method for declaring argv is shown here.

char *argv[];

The empty brackets indicate that it is an array of undetermined length. All command-line arguments are passed to main() as strings. To access an individual string, index argv. For example, argv[0] points to the program's name and argv[1] points to the first argument. This program displays all the command-line arguments that it is called with.

#include <stdio.h>

void main(int argc, char *argv[])

{

int i;

for (i=1; i&ltargc; i++) printf("%s",argv[i]);

}

The ANSI C standard does not specify what constitutes a command-line argument, because operatring systems vary considerably on this point. However, the most common convention is as follows:
Each command-line argument must be separated by a space or a tab character. Commas, semicolons, and the like are not considered separators. For example:
This is a test
is made up of four strings, but
this,that,and,another
is one string. If you need to pass a command-line argument that does, in fact contain spaces, you must place it between quotes, as shown in this example:
"this is a test"
A further example of the use of argc and argv now follows:
void main(int argc, char *argv[])
{
if (argc !=2) {
printf("Specify a password");
exit(1);
}
if (!strcmp(argv[1], "password"))
printf("Access Permitted");
else
{
printf("Access denied");
exit(1);
}
program code here ......
}
This program only allows access to its code if the correct password is entered as a command-line argument. There are many uses for command-line arguments and they can be a powerful tool.
My final example program takes two command-line arguments. The first is the name of a file, the second is a character. The program searches the specified file, looking for the character. If the file contains at least one of these characters, it reports this fact. This program uses argv to access the file name and the character for which to search.

/*Search specified file for specified character. */

#include <stdio.h>

#include <stdlib.h>

void main(int argc, char *argv[])

{

FILE *fp; /* file pointer */

char ch;

/* see if correct number of command line arguments */

if(argc !=3) {

printf("Usage: find >filename> <ch>\n");

exit(1);

}

/* open file for input */

if ((fp = fopen(argv[1], "r"))==NULL) {

printf("Cannot open file \n");

exit(1);

}

/* look for character */

while ((ch = getc(fp)) !=EOF) /* where getc() is a */

if (ch== *argv[2]) { /*function to get one char*/

printf("%c found",ch); /* from the file */

break;

}

fclose(fp);

}

The names of argv and argc are arbitrary - you can use any names you like. However, argc and argv have traditionally been used since C's origin. It is a good idea to use these names so that anyone reading your program can quickly identify them as command-line parameters.


Learn More :