If you wish to implement command line arguments c++ program, then  C++ language has an inbuilt function that looks like this:

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

The first keyword argc is the number of arguments that are passed into the program from the command line. Argc will always be at least 1 because the first argument is always the name of the program. Each command line argument that is provided, will make argc increase by one.

The second keyword argv is where the actual arguments are stored. You can use each argv element just like a string or as a two-dimensional array. The use of argv may be confusing at first but they are really just an array of C-style strings.

Tech Tip: Did you know how you can code from anywhere and anytime by installing Code editors, IDEs, or scripting tools on a virtual desktop from www.CloudDesktopOnline.com and access it remotely from any of your favorite devices (PC/Mac/Android/iOS). To learn more about such top-notch cloud solutions visit Apps4Rent.com.

Have a look at this example code which has implemented command line arguments c++. Copy and paste it into Visual Studio and play around with entering command line arguments to see what the program does.

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 // The default command line
 if(argc == 1)
 {
 cout << "This is testing out command line arguments\n";
 }

 if (argc == 2) 
 { 
 if(argv[1][0] == 'P')
 {
 cout << "The P Command has been entered in\n";
 }

 else if(argv[1][0] == 'A')
 {
 cout << "The A command has been entered in\n";
 }
 else if(argv[1][0] == 'L')
 {
 cout << "The L command has been entered in\n";
 }
 else
 // If none of the commands are P, A or L
 cout << "None of the commands are P, A or L\n";
 }

 return 0;
}

Now you may be asking, how can input the commands I have implemented in this program, as every time you run the program, it shows the first default argument “This is testing out command line arguments”. To input command line arguments in the program, follow these steps in visual studio.

1. Right-click on the second line in solution explorer in visual studio and choose properties.

cla step 1

2. Click on Configuration Properties and choose Debugging

cla step 2

3. In the second option, “Command Arguments” is now where we can enter our command line arguments.

cla step 3

4. Enter in the letter which you want the program to execute. Once you have entered your command line arguments, click ok.

5. Close the window and run the program with Control + F5 or start without debugging.

6. Now the program is running and executing command line arguments.

cla step 4

Tech Tip: Did you know how you can code from anywhere and anytime by installing Code editors, IDEs or scripting tools on a virtual desktop from www.CloudDesktopOnline.com and access it remotely from any of your favorite devices (PC/Mac/Android/iOS)? To learn more about such top-notch cloud solutions visit Apps4Rent.com.

Share this post

FAQ's

What are command line arguments?

Command line arguments in C++ are extra commands the user can enter in when running a program so that the functionality of the program changes

Why should we use command line arguments?

We should use command line arguments so that we can control our program externally and not internally by having to implement hardcoded features and recompile the code.

What is argc?

Argc in command line arguments is the number of argument commands is taking in during the command line argument process.

What is argv?

Argv is the number of strings pointed to by argv. It can simply be known as C-style strings declared which is an “argument vector”