Command line arguments in C / C++

Filed Under: C++
Command Line Arguments C Cpp

In this article, we’ll be looking at how we can use command line arguments in C / C++.

Command Line arguments are very useful if you want to pass any input strings to your main program, from the command line.

These arguments are passed as parameters to the main() function.

Let’s look at how we can use these effectively.


Why should we use command line arguments?

Often, it is very convenient for us to directly give input to our program. One common way is to use scanf() / getchar(), etc to wait for a user input.

But, these calls waste a lot of time in waiting, and requires the user to manually enter the input.

We can save a lot of time by simply giving these inputs to our main program!

The format will be something like:

./executable input1 input2

The program will automatically store those command-line arguments in special variables, from which we can access them directly!

This will only require a one time input, given when we start our program.

Let’s look at how we can use them now.

Command Line Arguments in C/C++ – The special variables

The program will pass the command line arguments to the main() function.

In C / C++, the main() function takes in two additional parameters for these arguments.

  • argc -> Argument Count. Gives the number of arguments that we pass (includes the program name also)
  • argv -> Argument Vector. This is a char* array of strings. These are the argument values itself.

So, argv[0] is the name of the program itself, and argv[1]argv[argc-1] will be all our command line arguments.

int main(int argc, char* argv[]);

To see this in action, let’s take an example.


Using command line arguments – A simple example

Let’s consider a program which concatenates two strings, given as input.

We’ll pass in two command line arguments to our program, so our total argc must be 3 (including the program name).

We can write our program like this:

#include <iostream> 
#include <string>

using namespace std;

string concat_strings(string s1, string s2) {
    return s1 + s2;
}

int main(int argc, char* argv[]) 
{ 
    cout << "You have entered " << argc 
         << " arguments:" << "\n";

    if (argc != 3) {
        cerr << "Program is of the form: " << argv[0] << " <inp1> <inp2>\n";
        return 1;
    } 

    string result = concat_strings(argv[1], argv[2]);

    cout << "Result: " << result << endl;

    return 0; 
} 

If our executable name was test.out, on my linux machine, I run the executable using this command:

./test.out Hello _JournalDev

Notice that the arguments are space-separated. So our command-line arguments are: “Hello” and “_JournalDev”

Output

You have entered 3 arguments:                                                                                                
Result: Hello_JournalDev

Great! This seems to work as expected, since the first argument is the name of the program itself.

Let’s try to run this with 4 arguments now.

./test.out Hello from JournalDev

Output

You have entered 4 arguments:                                                                                                
Program is of the form: ./test.out <inp1> <inp2>

Indeed, it gives us the correct error message!


Conclusion

Hope this article gives you a better understanding of command line arguments. We saw how we can use it to make our lives easier!

For similar content, do go through our tutorial section on C++ programming.

References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages