# C Command Line Arguments

In C, the most important function is the main() function. We mostly define the main() function either with a return type int or without the return type i.e. void. It is possible to pass certain numbers of parameters to your program from the command line. Command line argument is a very important concept in C programming. It is mostly used in the program at the time where you need to control your program from outside of the program. Command line argument is a parameter passed to the program when it gets invoked. We pass the command line arguments to the main() method. Following syntax represents how we can pass the command line arguments in our main function.

int main (int argc, char *argcv[] )

For passing the command line to our function, we define the main() function with two arguements. The first argument so passed ‘argc’ is ‘argument count’ will hold the number of command line arguments and is of type int. Likewise, the second argument is an array of character pointers listing all the arguments passed in the program. The ‘argcv’ stands for the argument vector which holds a pointer of type char.

While passing arguments, you can pass all the arguements which are separated by a space. But if there is a space in the argument itself then you can pass such arguements by putting them inside either double quotes “” or single quotes ”.

Now see the example below. This example will help you to understand the working of Command line arguements in C in more detail.

## Example of C command line arguement

The following program is a simple program works as an illustration of the command line argument in C. This program will take the arguements from the users and displays the output based on the number of arguements supplied to the program.

Code:

#include <stdio.h>

int main( int argc, char *argv[] )  {
int count;
printf(" Program name is %s", argv[0]);

if( argc == 2 ) {
printf("\n No of arguements passed is: ", argc);
printf("The argument passed is %s\n");
for(count=0; count<argc; count++)
printf("\nargv[%d]: %s",count,argv[count]);
}
else if( argc > 2 ) {
printf("\n More than necessary arguments passed.\n");
}
else {
printf("\n One argument expected as there is no other extra parameter other than program.\n");
}
}

Outputs in different conditions.

There are different conditions that the system will encounter while displaying the output based in the arguements passed in the program. The different conditions along with the outputs are displayed below.

One argument:

When we pass a single argument to the code above, After the compilation and execution of the program it produces the following result.

$./a.out first The argument passed is first Two arguements: If we pass two arguements in the above-compiled code will display its output as below.$./a.out first second
More than necessary arguments passed.

No arguements:

When we pass no parameters in the above code then it will display the following output after the compilation and the execution of the program.

\$./a.out
One argument expected as there is no other extra parameter other than the program.

You should always keep in your mind that argv[0] holds the name of the program and argv[1] points at the first line argument and argv[n] will give the last argument supplied. If there are no arguements supplied, then the default value of argc will be 1.

SHARE C Command Line Arguments