Updated April 11, 2023
Introduction to Command Line Arguments in C
The values passed to the C program from the command line when the program is executed are called command-line arguments in C. Command line arguments are important to the program when we want to pass the values to the program from outside and do not want to use it inside the code. The main() function in the program handles the command line arguments where the number of arguments passed to the program is represented by argc and every argument passed to the program is pointed by a pointer which is maintained in a pointer array represented by argv[].
The syntax :
int main(int argc, char *argv[])
where argc represents the count of arguments passed to the program and argv[] is a pointer array holding the pointers of data type char pointing to the arguments passed to the program.
Working of Command-Line Arguments in C
- Whenever there is a need to pass the values to the program from outside and do not want to use it inside the code, we make use of Command-Line Arguments in C.
- The values passed to the C program from the command line when the program is executed are called command-line arguments in C.
- The main() function in the program handles the command line arguments passed to the program when the program is executed.
- The number of arguments passed to the program is represented by argc and every argument passed to the program is pointed by a pointer which is maintained in a pointer array represented by argv[].
Examples of Command-Line Arguments
here are the following examples mention below:
Example #1
This program to demonstrate the use of Command-Line Arguments in a program to display the arguments passed to the program.
Code:
#include <stdio.h>
#include <conio.h>
//main method is called to which the command line arguments are passed to the program
int main(int argc, char *argv[])
{
//an integer variable is defined
int a;
//if condition is applied to check if the count of arguments passed to the program is greater than or equal to two and if the condition is true, the command line arguments passed to the program is printed otherwise no argument is passed to the program is printed
if( argc >= 2 )
{
printf("The arguments passed to the program are:\n");
for(a = 1; a < argc; a++)
{
printf("The argument passed to the program is: %s\t", argv[a]);
}
}
else
{
printf("No argument is passed to the program\n");
}
return 0;
}
Output:
In the above program, the main method is called to which the command line arguments are passed to the program. Then an integer variable is defined. Then if condition is applied to check if the count of arguments passed to the program is greater than or equal to two and if the condition is true, the command line arguments passed to the program is printed otherwise no argument is passed to the program is printed. We are not passing any command-line arguments to the program, hence the output no argument is passed to the program is printed on the screen.
Example #2
C program to demonstrate the use of Command-Line Arguments in a program to display the arguments passed to the program.
Code:
#include <stdio.h>
#include <conio.h>
//main method is called to which the command line arguments are passed to the program
int main(int argc, char *argv[])
{
//an integer variable is defined
int a;
//if condition is applied to check if the count of arguments passed to the program is greater than or equal to two and if the condition is true, the command line arguments passed to the program is printed otherwise no argument is passed to the program is printed
if( argc >= 2 )
{
printf("The arguments passed to the program are:\n");
for(a = 1; a < argc; a++)
{
printf("The argument passed to the program is: %s\t", argv[a]);
}
}
else
{
printf("No argument is passed to the program\n");
}
return 0;
}
Output:
In the above program, the main method is called to which the command line arguments are passed to the program. Then an integer variable is defined. Then if condition is applied to check if the count of arguments passed to the program is greater than or equal to two and if the condition is true, the command line arguments passed to the program is printed otherwise no argument is passed to the program is printed. We are not passing Hello as command-line arguments to the program.
Advantages of Command-Line Arguments in C
There are several advantages mentioned below :
- Whenever there is a need to pass the values to the program from outside and do not want to use it inside the code, Command Line Arguments can be used in C.
- The program to be executed can be controlled from the outside than hard-coding the values inside the program by making use of Command-Line Arguments.
Conclusion
In this tutorial, we understand the concept of Command-Line Arguments through definition, syntax, and working of Command-Line Arguments through programming examples and their outputs and the advantages of using Command Line Arguments in our program to control the program from outside than hard-coding the values inside the program.
Recommended Articles
This is a guide to Command Line Arguments in C. Here we discuss the working of Command-Line Arguments in C along with programming examples and advantages. You may also have a look at the following articles to learn more –