Command Line Arguments in C++
Last Updated :
26 May, 2025
Command-line arguments are arguments that are passed to a program when it is executed from the command line or terminal. They are provided in the command-line shell of operating systems with the program execution command.
The main function of C++ generally has the following signature:
C++
int main(){
// Function body
return 0;
}
But to pass command-line arguments, we typically define main() with two arguments, where the first argument is the number of command-line arguments and the second is the list of command-line arguments.
Signature of main() Function for Command Line Arguments
C++
int main(int argc, char *argv[]){
// Function body
return 0;
}
What is argc?
The variable argc (ARGument Count) is an integer that stores the number of command line arguments passed to the main function. It also includes the count for the name of the program, so if we pass a value to a program, the value of argc would be 2 (one for argument and one for program name).
What is argv?
The array argv (ARGument Vector) is an array of C-style strings like ('char*') where every element points to a command line argument. argv does not store the actual argument, but the pointer to that argument. The argv[0] will always contain the name of the program.
Example of Command Line Argument
C++
#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 geeks
Output
You have entered 3 arguments:
Argument 1: ./program1
Argument 2: hello
Argument 3: geeks
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.
C++
#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 three
Output:
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./program
argv[1]: one
argv[2]: two
argv[3]: three
In the above program, the argv[0] points to the first command line argument and argv[argc-1] points to the last argument.
Note: Only string values are passed through the command line argument and argv[argc] is NULL.
Passing Space Seperated String as a Single Argument
In C++ program, multiple command line arguments are passed to the function by separating them by a whitespace but what happens if you have to pass an argument string that already contains spaces. In such case, we can enclose that string in the double quotes to pass it as a single argument.
C++
#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
Similar Reads
C++ Programming Language
C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Object Oriented Programming in C++
Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Inheritance in C++
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read