In C++ (and C) programs, command line arguments are passed in as arguments to the main function. Command line arguments are useful as they allow users to pass data values into programs when they launch the program on the command line. Consider a simple program to calculate the area of a circle. For purely educational purposes, the radius is usually hardcoded in the source file which is an extremely poor way for a real-world user to use the program. An alternative is for the user to enter it on the command line via the keyboard when prompted to by the program. Now in many real-life scenarios, a user does not interact directly with a program via the command line, but instead with a script (can be written in Bash or Python etc.) that executes multiple different programs, or runs the same program multiple times. For example, you may wish to calculate the area of 100 different circles. You would not want to have to run the program manually 100 times and enter each value individually. In this case, you would create a script that loops over the 100 different input values, passes them into the program via command line arguments and probably print the output (and input) to a text file.
Command line arguments are passed in via arguments to the main function.
1 |
int main(int argc, char const *argv[]) |
Here, argc is the number of command line arguments passed in and *argv[] is basically an array of C-style strings that were entered onto the command line. A simple C++ program that prints command line arguments is shown below.
main.cpp
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main(int argc, char const *argv[]) { std::cout << "Number of arguments : " << argc << std::endl; for (int i = 0; i < argc; i++) { std::cout << "argv[" << i << "] = " << argv[i] << std::endl; } return 0; } |
Now, suppose the program is compiled and run via the command line as follows:
./main.exe Hello, my name is Craig 1.234
It will produce the following output:
Number of arguments : 7 argv[0] = ./main.exe argv[1] = Hello, argv[2] = my argv[3] = name argv[4] = is argv[5] = Craig argv[6] = 1.234
He we can see that 7 different arguments have been counted. Note that the first argument (the zero-index in the array) is the name of the program, as this was the first thing typed on the command line. Sometimes this may be prefixed by the path to the executable. Each subsequent argument is then determined by the space between them. To enter a string via the command line arguments, double quotes must be used e.g.
./main.exe "Hello, my name is Craig" 1.234
argv[0] = ./main.exe argv[1] = Hello, my name is Craig argv[2] = 1.234
Also note that although we have entered a number, it will not be interpreted by the compiler as a float as it is a C-style string. We need to convert any numbers contained in strings to the relevant data type before we use them in calculations.
The example below demonstrates how to convert numbers in strings into numeric data types so that they can be used in calculations. Here we enter the radius of the circle on the command line and the program will convert it to a double before calculating the area and printing it to the command line.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <cmath> #include <iostream> const double PI = 3.14159265359; int main(int argc, char const *argv[]) { if (argc > 1) { double radius = atof(argv[1]); double area = PI * pow(radius, 2.0); std::cout << "Circle area = " << area << " m^2.\n"; } else { std::cerr << "Error! You haven't inputted a radius!\n"; return 1; } return 0; } |
First, note that the value of pi is stored in a const double. The use of the const keyword prevents the user from modifying the value later in the program. This is the more 'C++ way' of handling constant values rather than #define pre-processor directives which are more a legacy from the C programming language.
We then check whether the user entered more than one argument. There will always be at least one, as the user must type in the name of the executable to run the program. If they do not enter a radius for the circle, we prompt them to using cerr which is the output stream usually used for error messages and end execution of the program by returning a value of 1. This will notify the OS that the program did not finish executation correctly.
If they have entered a radius, we convert it to a double using the atof function. We can also use atoi to convert a string to an integer. The area is then calculated and printed to the terminal.
The user would interact with the program as follows:
./main.exe 1.0
The following output would be displayed.
Circle area = 3.14159 m^2.