What is argv?
argv (argument vector) is an array of character pointers (char* argv[]) that stores the actual command-line arguments as strings, where argv[0] is the program's name.
What is a command line argument in C++?
Command Line Arguments are values or parameters passed to a program through the command line or terminal when it is executed. They allow users to customize program behaviour or pass data to a program without modifying the code. Command Line Arguments can be used in various programming languages and are commonly used in command-line utilities and scripts.
Example
int main( int argc, char *argv[] )
argc - Number of arguments passed
argv[] - A pointer array which points to each argument passed in the program
Example of Command Line Argument in C++
Let us run this code on our Linux machine.
// Name of program commandline.cpp
#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
Input:
$ g++ commandline.cpp -o main
$ ./main coding is fun
Output:
You have entered 4 arguments:
./main
coding
is
Fun
Properties of Command-Line Arguments in C++
- They are passed to the main() function.
- They are parameters/arguments supplied to the program when it is invoked.
- They are used to control program from outside instead of hard coding those values inside the code.
- argv[argc] is a NULL pointer.
- argv[0] holds the name of the program.
- argv[1] points to the first command-line argument and argv[n] point the last argument.
Note: You pass all the arguments separated by a space. If the argument itself has space, then you can pass such arguments by putting them inside double quotes “or single quotes.”
Example
#include<stdio.h>
int main(int argc,char* argv[])
{
int counter;
printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
if(argc>=2)
{
printf("\nNumber Of Arguments Passed: %d",argc);
printf("\n----Following Are The Command Line Arguments Passed----");
for(counter=0;counter<argc;counter++)
printf("\nargv[%d]: %s",counter,argv[counter]);
}
return 0;
}
Output of Command Line Argument in Diferent Scenarios
- Without argument: When the above code is compiled and executed without passing any argument. It produces the following output.
$ ./a.out
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Programme Name
- Three arguments: When the above code is compiled and executed with three arguments. It produces the following output.
$ ./a.out First Second Third
Program Name Is: ./a.out
Number Of Arguments Passed: 4
Following Are The Command Line Arguments Passed
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third
- Single Argument: When the above code is compiled and executed with a single argument separated by space but inside double quotes. It produces the following output.
$ ./a.out “First Second Third”
Programme Name Is: ./a.out
Number Of Arguments Passed: 2
Following are the Command Line arguments passed
argv[0]: ./a.out
argv[1]: First Second Third
- The single argument in quotes separated by space: When the above code is compiled and executed with a single argument separated by space but inside single quotes. It produces the following output.
$ ./a.out ‘First Second Third’
Program Name Is: ./a.out
Number Of Arguments Passed: 2
Following Are The Command Line Arguments Passed
argv[0]: ./a.out
argv[1]: First Second Third
getopt() Function
It is a function in C to pass command line arguments.
Return Value: The getopt() function returns different values:
- If the option takes a value, that value is a pointer to the external variable optarg.
- ‘-1’ if there are no more options to process.
- ‘?’ when there is an unrecognized option and it stores into external variable optopt.
- If an option requires a value (such as -f in our example) and no value is given, getopt normally returns? By placing a colon as the first character of the options string, getopt returns: instead of? when no value is given.
Generally, the getopt() function is called from inside of a loop’s conditional statement. The loop terminates when the getopt() function returns -1. A switch statement is then executed with the value returned by getopt() function.
Syntax:
getopt(int argc, char *const argv[], const char *optstring)
optstring is simply a list of characters,
each representing a single character option.
How To Read/Get Command Line Arguments?
In C++, the main function can take two arguments: argc and argv. Which represent the number of command line arguments and an array of character pointers representing the actual arguments, respectively.
To access individual arguments, you can simply index into the argv array, starting from 0. For example, argv[0] will contain the name of the program being executed. argv[1] will contain the first command line argument.
You can use a for loop to iterate through all the command line arguments, with argc as the loop counter.
Frequently Asked Questions
How to run command-line arguments in C in Dev C++?
To run command-line arguments, first, write a C program that accepts command-line arguments. Then, in Dev-C++, go to the "Project" menu, select "Project Options", click on the "Parameters" tab, and enter the command-line arguments in the "Parameters" box. Finally, run the program.
How do I create a command line argument?
To create a command line argument, simply pass a value or parameter after the program name when executing a program in the command line or terminal. The program can then retrieve and use these values as arguments during its execution.
What is the use of the command line?
A command line is a text-based interface used to enter commands and interact with a computer system. It's a powerful and efficient way to run programs, execute tasks and automate repetitive tasks, and it is available on various operating systems.
Conclusion
In this article, we discussed Command Line Arguments in C++. We also saw the approach and Java code to solve it. Hope this article helped you to understand this question in an easy way and motivated you through your coding journey.
Check out other related articles to learn more: