Command-line arguments are values passed to a program when it is executed from a terminal or command prompt. They allow users to provide input, configuration options, or file names at runtime without modifying the program's source code.
- Command-line arguments are received through the argc and argv parameters of the main() function.
- They make programs more flexible by allowing different inputs during execution.
Normally, a C++ program starts execution from the main() function:
int main(){
// Function body
return 0;
}
To access command-line arguments, main() is defined with two parameters:
int main(int argc, char *argv[]){
// Function body
return 0;
}
Parameters of Command Line Arguments
When command-line arguments are used, the main() function receives two parameters: argc and argv. Together, they provide information about the number of arguments supplied and the values of those arguments.
argc (Argument Count)
argc is an integer that stores the total number of command-line arguments passed to the program.
- It includes the program name in the count.
- If no additional arguments are provided, argc is 1.
- If one argument is passed, the value of argc becomes 2.
argv (Argument Vector)
argv is an array of C-style strings (char*) that stores the command-line arguments.
- argv[0] contains the name or path of the program.
- argv[1] contains the first command-line argument.
- argv[argc - 1] contains the last argument passed to the program.
- argv[argc] is always nullptr.
Example of Command Line Arguments
The following program prints all command-line arguments passed to it:
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "You have entered " << argc
<< " arguments:" << endl;
// Using a while loop to
// iterate through arguments
int i = 0;
while (i < argc) {
cout << "Argument " << i + 1
<< ": " << argv[i]
<< endl;
i++;
}
return 0;
}
Input
./program1 hello geeksOutput
You have entered 3 arguments:
Argument 1: ./program1
Argument 2: hello
Argument 3: geeks
Explanation
The program receives three arguments:
- argv[0] → ./program1
- argv[1] → hello
- argv[2] → geeks
Since three arguments are present, argc becomes 3.
Command Line Arguments in Different Scenarios
The following programs illustrate the behaviour of C++ program for different kinds of command line arguments.
Multiple Command Line Arguments
In the following program, we pass three arguments to the main function from the command line.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
if (argc >= 2) {
// printing number of arguments
cout << "Number Of Arguments Passed: " << argc
<< endl;
cout << "----Following Are The Command Line "
"Arguments Passed----"
<< endl;
// printing all the arguments
for (int i = 0; i < argc; ++i) {
cout << "argv[" << i << "]: " << argv[i]
<< endl;
}
}
return 0;
}
Terminal Command:
$ ./program one two threeOutput
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./program
argv[1]: one
argv[2]: two
argv[3]: three
Explanation
The program name is also counted as an argument, so four arguments are received in total.
- argv[0] → Program name
- argv[1] → one
- argv[2] → two
- argv[3] → three
Note: Command-line arguments are passed as strings. The element argv[argc] is always nullptr.
Passing Space Seperated String as a Single Argument
Arguments are normally separated by whitespace. If a string contains spaces and should be treated as a single argument, enclose it within quotes.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout << "Program Name Is: " << argv[0] << endl;
if (argc >= 2) {
cout << "Number Of Arguments Passed: " << argc
<< endl;
cout << "----Following Are The Command Line "
"Arguments Passed----"
<< endl;
cout << "argv[0]: " << argv[0] << endl;
cout << "argv[1]: " << argv[1] << endl;
}
return 0;
}
Terminal Command:
$ ./solution.out "one two three"Output
Program Name Is: ./solution.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./solution.out
argv[1]: one two three
Explanation
Although the string contains spaces, the shell treats everything inside the quotes as a single argument. As a result:
- argv[0] contains the program name.
- argv[1] contains the complete string "one two three".
Without quotes, the shell would split the text into three separate arguments.