Command line arguments

  1. Courses


  2. Complete C++ Course


  3. The basics


  4. Command line arguments

The arguments of the main function

Until now, we have defined the main function without arguments. It is however possible to define it with an integer and an array of string as arguments. In that case, if the program is called from the command line and is given parameters, the main function will receive them through its arguments.

Here is the header of a main function with arguments:

int main(int argc, char **argv)

The following is equivalent to the above:

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

The second argument is an array with the parameters (as null-terminated strings) the program received. The first argument is the number of parameters received. The program always receives the path of the executable of the program as first parameter. So it always has at least one parameter. When the program is called from the command line, the words following the name of the executable are taken as parameters.

Accessing the parameters received

Here is a program printing the parameters received, to the console:

#include <iostream> int main(int argc, char *argv[]) { for(unsigned c = 0; c < argc; c++) std::cout << argv[c] << std::endl; return 0; }

By executing the program directly from the IDE, the output is the following:

Note that if we execute it by double-clicking on the executable file, the result will be similar, but we will not have the time to see the output as the console will close immediately at the end of the execution of the main function.

Introduction to the command line on Windows

The command line

The command line is an interface used to access the features of an operating system through a console interface. Some operating systems, especially the ones designed for servers, only have a console interface and no graphical one.

Opening the command line

On Windows, the command line can be opened by writing cmd.exe in the search box of Windows and pressing the key enter.

Using the command line

At the left of the console, there is the path, inside the file system, where the command line currently is:

We can change the drive the command line is in by writing its letter followed by a colon and then pressing the key enter:

We can use the command cd followed by the name of a folder to go inside it:

We can use the command cd followed by two dots to go to the parent folder of the current folder:

We can give many folders, separated by slashes, to the command cd, to directly go to that location:

We can execute a program, located in the folder the command line currently is in, by entering its name (With or without the .exe extension):

Here, the program 'NewCppProject.exe' simply prints the string "Hello!\n" to the console.

Giving parameters to the program

If we open a command line, go to the folder where the executable of the example above is and execute the program from it, the output will be the name of the executable file.

The reason is that the first 'word' from the command line is taken as the first argument (Which is NewCppProject.exe here). In that case, the first parameter is not an absolute path to the executable file, but a relative one (From the current folder).

Let us execute the program by writing words after the name of the executable:

Each word has been taken as one parameter. If we want the whole sentence to be considered as one parameter, we must write it between quotes: