Command Line Arguments in C++

Command-line arguments are the values that are passed in your C++ programs when they are executed. The purpose of using command line arguments is to control the execution of your program from outside. In this post, we will learn about these arguments in detail.

Command line arguments in C++

As we know, in most of our C++ program we use main() function without parameter. But command-line arguments are passed using main() function with parameters. To pass arguments in command line we need to define main() function with two parameters like this:

int  main( int argc, char *argv[]) {/* some code……..*/}

or

int main( int argc, char **argv) {/*some code………*/}

 

1.argc: This is an integer and represents the count of command-line arguments including the program name.
2.argvThis represents a pointer to a list of arguments passed. The first element of argv[] array which is referred by this pointer is the name of the program. Therefore, if we want to pass one command line argument, we need to pass 2 for argc. One for the program name and one for the argument. After argv[0], every element till argv[argc-1] is a command-line argument.

See the code for a better understanding.

Implementation of command line arguments in C++ :

//commandlineexample.cpp
#include <iostream>
using namespace std;

int main(int argc, char *argv[])                                                    
{                                                                                   
    cout<< "Total number of arguments are: " << argc <<endl;                        
    cout<< "Your program name is: " << argv[0] << endl;                             
                                                                                    
    cout << "You have entered arguments:" <<endl;                             
    for(int i=1;i<argc;i++)                                                         
    {                                                                               
       cout<<argv[i]<<endl;                                                         
    }                                                                               
    return 0;                                                                       
}

Output:

[root@localhost Desktop]# g++ commandlineexample.cpp -o commandlineexample 
[root@localhost Desktop]# ./commandlineexample Codespeedy, you are doing great.
Total number of arguments are: 6
Your program name is: ./commandlineexample 
You have entered arguments: 
Codespeedy, 
you 
are 
doing 
great.

Explanation: In the above program, we have passed 5 arguments. the program name is added as an argument in argv[] array at the beginning of the array and argument count becomes 6. Also, you can see that in the program, the compiler assumes every space-separated string as a different argument. If we want to pass a string with spaces as a single argument we have to use single or double quotes as shown here.

[root@localhost Desktop]# ./commandlineexample Codespeedy, 'You are doing great.'   
Total number of arguments are: 3                                                    
Your program name is: ./commandlineexample                                          
You have entered arguments:                                                   
Codespeedy,                                                                         
You are doing great. 

If we don’t pass any command line argument

See what happens when you don’t pass any command-line argument.

[root@localhost Desktop]# ./commandlineexample                                      
Total number of arguments are: 1                                                    
Your program name is: ./commandlineexample                                          
You have entered arguments:  

As you can see, since no arguments were passed, argv[] has only the program name as its element.

Thank you.

Also read: Postfix to Prefix Conversion in C++

Leave a Reply