Javatpoint Logo
Javatpoint Logo

Command line arguments in C++

Command-line arguments are a fundamental concept in programming that allows developers to provide input parameters to a program when executed. In C++, the main function can accept command-line arguments, enabling programmers to create more versatile and interactive applications. In this article, we will delve into the intricacies of command-line arguments in C++, their usage, and their significance in software development.

Understanding Command-Line Arguments

In C++, command-line arguments are values passed to a program during its execution from the command-line interface (CLI). These arguments are separated by spaces and are provided after the program's name. The C++ standard library facilitates handling command-line arguments through the main function's parameters: int argc and char* argv[].

  • argc:

The argc[] stands for "argument count". It holds the number of command-line arguments passed to the program.

  • argv[]:

The argv[] stands for "argument vector". It is an array of character pointers that point to the strings representing the individual arguments.

Importance of Command-Line Arguments

There are several importances of command-line arguments. Some main importances are as follows:

  • Flexibility and User Interaction:

Command-line arguments provide a way to interact with programs directly from the terminal or command prompt. This interaction enhances the program's flexibility, as users can modify its behavior without modifying the source code. For instance, a data analysis tool could accept parameters like data source, analysis type, and output format as command-line arguments, making it adaptable to various use cases.

  • Scripting and Automation:

Command-line arguments are essential for scripting and automation. Developers can create scripts that execute programs with specific arguments, automating repetitive tasks. It is particularly useful for tasks like batch processing, where the same program needs to be executed multiple times with different inputs.

  • Program Configuration:

Command-line arguments allow users to configure program settings without recompiling the code. It is particularly valuable for programs that are distributed to users who might have different preferences or requirements. Configuration options like debug mode, verbosity level, and output directory can be controlled through command-line arguments.

  • Testing and Debugging:

During software development, command-line arguments facilitate testing and debugging. Developers can simulate different scenarios by passing specific arguments, aiding in identifying bugs and errors. It reduces the need to modify code for different test cases.

  • Standardization and Conventions:

The use of command-line arguments follows established conventions, making programs more intuitive and user-friendly. Users familiar with command-line interfaces expect to pass arguments in a standardized way. This consistency simplifies the learning curve for new programs.


Let's take a program to demonstrate the command line arguments in C++.


Command line arguments in C++


main Function:

  • The main function is where the program starts its execution.
  • It takes two arguments: int argc (argument count) and char* argv[] (argument vector). These parameters enable the program to receive command-line arguments.

Argument Validation:

  • The program checks if the number of arguments provided is less than 3. It accounts for the program name and two numbers we expect as input. If insufficient arguments are provided, the program displays a usage message and returns 1, indicating an error.

Argument Conversion:

  • The atoi function converts the command-line arguments (which are strings) to integers.
  • The converted integers are stored in the variables number1 and number2.

Performing Operations:

  • The program calculates the sum of number1 and number2 and stores it in the sum
  • Similarly, the product of number1 and number2 is calculated and stored in the product variable.

Printing Results:

  • The program displays the original values of number1 and number2, as well as the computed sum and product.

Return Statement:

  • Finally, the program returns 0, indicating successful execution. A return value of 0 conventionally signifies that the program ran without errors.

The program demonstrates how to process command-line arguments in C++ by following this structure. It takes two numbers as input, performs addition and multiplication, and then displays the results. Using namespaces, command-line argument handling, and basic arithmetic operations showcases fundamental programming concepts in C++.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA