Open In App

How to Parse Command Line Arguments in C++?

Last Updated : 27 Feb, 2024
Like Article

In C++, command line arguments are parameters that are passed to a program when it is invoked via command line or terminal. In this article, we will learn how to parse command line arguments in C++.

Parse Command Line Arguments in C++

In C++, we can parse the command-line arguments using the argc and argv[] parameters that are passed to the main function.

  • argc represents the number of command line arguments passed to the program,
  • argv is an array of C-style strings (character arrays) containing the actual arguments.

We can use loops to iterate and parse the command line arguments stored inside the argv array.

C++ Program to Parse Command Line Arguments


// C++ Program to illustrate how to Parse Command Line
// Arguments
#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;
    return 0;


./program1 this is something


You have entered 4 arguments:
Argument 1: ./program1.out
Argument 2: this
Argument 3: is
Argument 4: something

Previous Article
Next Article

Similar Reads

Command line arguments example in C
Prerequisite: Command_line_argument. The problem is to find the largest integer among the three using command line arguments. Notes: Command-line arguments are given after the name of the program in the command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments: the first argument is the n
3 min read
Command Line Arguments in C++
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 can have the following signature: int main(){ // Suitable Code return 0; } But to pass comman
4 min read
How to Retrieve Command-Line Arguments in C++?
Command-line arguments are the values or parameters that are passed after the name of the program through the command line or terminal when the program is executed. In this article, we will learn how to retrieve command-line arguments in C++. Retrieving Command Line Arguments in C++To access all the command line arguments that are passed from the c
2 min read
How to create a command-line progress bar in C/C++
The task is to write a C/C++ program to draw a command-line progress bar. Approach: To create a progress bar the idea is to use system() function which will give colored output. Below is the illustration of how to use system() function. The system function accepts the following parameters for colouring the output screen: keyword: colorBackground co
2 min read
Program to delete a line given the line number from a file
Given a file and a line number n, the task is to delete nth line from the given text file. Suppose the initial content of myfile.txt is : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Practice GeeksforGeeks Contribute After Deletion of Line 2 the content would be : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Contribute Approach : 1) Open the sour
2 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
How to Read a File Line by Line in C++?
In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by line from a particular stream. We can redirect the
2 min read
Program to Parse a comma separated string in C++
Given a comma-separated string, the task is to parse this string and separate the words in C++. Examples: Input: "1,2,3,4,5" Output: 1 2 3 4 5 Input: "Geeks,for,Geeks" Output: Geeks for Geeks Approach: Get the string in stream - stringstreamCreate a string vector to store the parsed wordsNow till there is a string in stringstream, checked by good()
1 min read
How to Read and Parse Json File with RapidJson?
RapidJSON is a high-performance JSON library for C++. It provides a fast and easy-to-use interface for parsing and generating JSON. It is small but complete. It supports both SAX and DOM style API. Also, it is self-contained and header-only. It does not depend on external libraries such as BOOST. It even does not depend on STL. Here is an example o
5 min read
How to Parse an Array of Objects in C++ Using RapidJson?
RapidJSON is an open-source C++ library for parsing and serializing JSON (JavaScript Object Notation) data. It is designed to be fast and efficient, with a focus on simplicity and ease of use. It is widely used in a variety of applications and is known for its fast performance and low memory overhead. It is a popular choice for parsing and serializ
4 min read
Article Tags :
Practice Tags :