Command Line Arguments In C++

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

A Brief Introduction To Command Line Arguments In C++.

We have already seen the usage of arguments or parameters in our tutorial on functions. We also learned the purpose of passing arguments to/from functions.

We can also have arguments passed to the main function. These are in turn known as ‘Command Line arguments or Command Line Parameters’.

=> Check Here To See A-Z Of C++ Training Tutorials Here.

Command Line Arguments in C++

What Are Command Line Arguments?

We know the basic prototype of the main function in C++. It usually has the return type as int and no arguments are passed to it.

int main()

However, we can also pass arguments to the main function of C++ which are known as Command Line Arguments. Command line arguments are given after the name of the program during the execution of the program in a command-line shell.

In order to pass command line arguments, the main function is passed with two arguments. The prototype of the main function then changes to

int main(int argc, char* argv[]){}

OR

int main(int argc, char** argv){}

The two arguments are described below:

#1) Argument Count (ARGC)

This is a non-negative integer argument that holds the number of command line arguments including the program name. Thus if pass a program name is passed then argc will have the value of 1.

#2) Argument Vector (ARGV)

Argv is an array of character pointers that contains all the command line arguments passed to the main function. If ARGC is greater than zero, then Argv[0] will contain the name of the program. Argv [1] to argv [argc -1] will contain the other command line arguments.

How To Read/Get Command Line Arguments?

Having seen the parameters that hold count and actual command line arguments, let us see how we can use command line arguments in a C++ program.

Note that we need to run the program from the command line shell in order to get the complete functionality of command line arguments.

First, let us see the output of the program where we do not specify any command line arguments.

#include <iostream> 
using namespace std; 

int main(int argc, char** argv) 
{ 
    cout << "Number of command line arguments (argc) entered: " << argc<<endl; 
      
    for (int i = 0; i < argc; ++i) 
        cout <<"argv["<<i<<"] : "<<argv[i] << "\n"; 
      

    return 0; 
}

The above code example shows how we can read and parse the command line arguments.

First, we print the number of command line arguments which is directly given by the first parameter to the main function, argc. Then using for loop, we loop through the argument vector argc which is a character array.

This loop runs from 0 to argc as argc is the total number of command line arguments that were passed to the program during execution.

Now we will execute the above program,

#1) Without Passing Command Line Arguments.

In this case, we execute the above program using the following command:

$ ./a.out

Here, we simply execute the program without any command line arguments. The output is shown below. In this case, as no arguments are provided, only the program name is taken and the argc displays 1 which is argv[0] that is the program name.

Output:

Number of command line arguments (argc) entered: 1

argv[0] : ./a.out

#2) Passing Three Command Line Arguments

In this case, we pass three arguments to the command line by giving the following command.

$ ./a.out one two three

Here we have given three command line arguments.

When we execute the above program with these arguments, we get the following output.

Number of command line arguments (argc) entered: 4

argv[0] : ./a.out

argv[1] : one

argv[2] : two

argv[3] : three

The above output shows argc value as 4. This includes the program name and the three arguments that we entered on the command line. If we see the argv array that we print, argv[0] is the program name and the subsequent array elements contain the three arguments that we passed.

Points to Remember

  • In command line arguments, argv[argc] is a NULL pointer.
  • Argv[0] always holds the program name.
  • Argv[1] holds the first command line argument while argv[n] is the last command line argument.
  • Command line arguments are passed to the main function.
  • We should pass command line arguments when the program is invoked or executed.
  • Command line arguments control the program from outside as we pass the arguments through the command line.

Conclusion

In this tutorial, we have seen the command line arguments of C++.

These are really useful when we need to control the program externally. Also instead of hardcoding some values in the program, we can use command line arguments to pass these values.

=> Check The Complete C++ Training Series Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment