What is Command Line Arguments in C Language
Hello friends, in today’s article we are going to talk about Command Line Arguments.
Today we will learn in detail about what is Command Line Arguments in C? And how to use Command Line Arguments in C language.
So without wasting any time let’s first understand what is Command Line Arguments in C.
Command Line Arguments in C
Arguments that are passed through the command line are called Command Line Arguments.
When we create a program in C language, we can run that program in three ways -:
- By IDE like – turbo c/c++ , devc, code blocks.
- by double clicking
- Through command line cells like – command prompt, dos, Linux
Most of you have run C language program only through IDE (turbo c/c+, code blocks) out of these three methods.
If you want, you can also run the program by double clicking on the .exe file which is obtained from the application of the program by the IDE and if you want, you can also run the program by typing the name of the program in the command prompt.
Here the command prompt is similar to dos os, in which we can easily run any program if we want and when we run a program by passing an argument in the command line cell, then this is called command-line argument.
Main() is an important function in C language, which is defined in most programs with void or int return type. in the main() function we do not write any argument in its parenthesis.
int main()
{
/*
Statements;
*/
}
As you know, in C language we can write arguments in the parenthesis of the function and since main() is also a function then we can also write some arguments in the parenthesis of the main() function.
Through Command Prompt (which is a command cell in the window) We can pass Argument to the main() function.
To pass command line arguments, we have to define the main() function in the program as follows.
Syntax:
int main(int argc, char *argv[] )
argc (ARGument Count) – It is of integer type and counts the number of arguments passed after the program name. If two arguments are passed to the main() function, then 2 will be stored in argc.
argv(ARGument Vector) – It is a character pointer, which contains the argument.
Let’s make a program to understand command line arguments better.
Example for Command Line Argument in C
// Name of program hello.c
#include <stdio.h>
int main( int argc, char *argv[] )
{
printf("You have entered %d arguments",argc);
for (int i = 0; i < argc; ++i)
printf("%s \n", argv[i]);
return 0;
}
Input:
When we run this program in the command line and pass 4 arguments (./main Welcome To CsTutorialpoint.com ).
output -:
./main
Welcome
To
CsTutorialpoint.com
Program Explanation
In this program, we passed four arguments to the main() function through the command line. The number of arguments are stored in argc and the arguments are stored in argv and then we print the arguments stored in argv by printf() function.
Properties of command line argument
- Through the command line, we pass arguments to the main() function.
- Arguments are passed to the program when the operating system calls the program.
- It is used to control the program from outside.
- argv[] is a null pointer.
- argv[0] Holds the name of the program.
- argv[1] is the first command line argument and argv[n] is the last command line argument.
Note – Command line arguments are separated by spaces and if there is a space in the argument itself, then that argument is passed by writing it in double quotes or single quotes.
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this article you will know very well about, what is Command Line Arguments in C.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.