The command line argument in C is a very important concept in C programming language which involves passing argument at a command prompt.

What is command line argument in C?

The arguments that are passed to main() at the command prompt are called as command line arguments.
These are the parameters that are supplied to the application program at the time of invoking for execution.
That is passing the argument which executing the program.
We write the C program which uses the arguments that are provided by command line at the time of execution.

The main function accepts two command line arguments namely args and argv.
1. The first parameters usually have the name argc(argument count) and have the data type int.
It holds the count of a total number of arguments passed to main function.
2. The second parameter has the name agrv(argument vector) and has the data type char *[] that is an array of pointers to strings and where every element of the array will hold the address of character strings.
It holds the address of all the arguments that are passed to main function from the command line.

*argv[] = is an array of pointer to string
argc = is an int whose value is equal to the number of strings on the command are passed to main().
the strings at the command line are stored in memory and address of the first string is stored in argv[0], address of second string stored in argv[1] and so on.
The argument argc is set to a number of strings given on the command line.

How to pass command line arguments in C?

To pass arguments from the command line while executing program we simply write arguments that we want to pass to the program.
Syntax to pass command-line arguments
java filename argument_1, argument_2 …. argument_N

Command line arguments in c example
Consider the following statement passing arguments to Test.c program from the command line.

Test C C++

In the above example, we are passing 2 arguments which are C C++ to program Test from a command prompt.
When this statement executed (when we run) theses arguments are assigned to the argc and argv[]
argc = 3
argv[0] = Test
argv[1] = C
argv[2] = C++

Whenever we pass arguments to main(), it is better to check whether the correct number of arguments have been passed on to main () or not.
After assigning arguments program is executed as we used these arguments in the program.
(Note : array index starts with Zero )
After assigning arguments program is executed as we used these arguments in the program.

Command line arguments in C program


int main(int argc,char* argv[])
int counter;
printf("Program Name Is: %s",argv[0]);
if(argc==1) // If no arguement passed at command prompt
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
if(argc>=2)  // Reading arguments from command prompt
printf("\n Command line arguments Of Arguments Passed: %d",argc);
printf("\nargv[%d]: %s",counter,argv[counter]);  //printing arguments
return 0;

Output : –

Command line arguments
=> Run the program from the command prompt.
=> While running the code pass some arguments.
Test C C++

Explanation: –
1. Passing the arguments from command prompt
Test C Cpp++
we passed 2 arguments while running program.

2. After passing arguments it go to main method
and arguments of main that is argv[] stores the command line arguments that we have passed.
argc=3 (including program name)

3. We print total number of arguments passed
printf(“\n Command line arguments Of Arguments Passed: %d”,argc);

4. Next we print arguments using for loop
At iteration 1
counter=0, 0<3 condition true
printf(“\nargv[%d]: %s”,counter,argv[counter]); => argc[0]=Test
At iteration 2
counter=1, 1<3 condition true
printf(“\nargv[%d]: %s”,counter,argv[counter]); => argc[1]=C
At iteration 3
counter=2, 2<3 condition true
printf(“\nargv[%d]: %s”,counter,argv[counter]); => argc[2]=C++
At iteration 4
counter=3, 3<3 condition false

5. End with the program.

Write A Comment