C++

Argc and Argv C++

While writing C++ programs, we all know that the “main()” function is considered very important since we cannot compile our program if the implementation of this function is missing. Just like all other functions in C++, the “main()” function is also capable of accepting arguments. However, the difference between passing arguments to the “main()” function from passing arguments to the other functions is that you have to pass the arguments through the command line in the former case. It is so because the “main()” function itself is the driver function which is why no other function is capable of calling it and passing arguments to it. In this article, we will be discussing the two parameters of the “main()” function, i.e., “argc” and “argv” in C++ in Ubuntu 20.04.

What is Argc and Argv in C++ in Ubuntu 20.04?

The parameter “argc” refers to the argument count, whereas “argv” refers to a character array that holds all the arguments that are passed to the “main()” function through the command line at the time of executing a program in C++. Here, you need to know that “argc” always shows argument count as “1” more than the actual number of the passed arguments. This is so because the name of the object file is also counted as a command-line argument. You can pass the command line arguments belonging to any data type to the “main()” function. All you need to take care of is that these parameters are mentioned in the prototype of your “main()” function if you want to access them inside it. However, the “main()” function can work perfectly well without these two parameters. This will be discussed in the following section of this article, after which we will proceed to the usage of these two parameters in C++ in Ubuntu 20.04.

The Main Function without Argc and Argv in C++:

First of all, we would like to tell you that the “main()” function in C++ can work perfectly well even without using the parameters “argc” and “argv”. This is depicted in the following C++ program:

We have a simple “main()” function in this program without any arguments. Within this “main()” function, we are only printing a sample message on the terminal.

Then, we compiled this basic C++ program with the help of the command mentioned below:

$ g++ CommandLine.cpp –o CommandLine

Afterward, we executed this program by running the following command:

$ ./CommandLine

The output of this simple C++ program is shown in the image below:

Running a C++ Program without Passing any Command Line Arguments:

Now, we will try to implement a C++ program in which the “main()” function is capable of accepting the parameters “argc” and “argv”, however, we will not be passing these arguments to it while executing this program from the terminal. The said C++ program is shown in the following image:

In this C++ program, our “main()” function is capable of accepting “argc” and “argv” parameters. However, since we did not intend to pass these values to it in this particular example, we have intentionally equalized “argc” to “0” so that when we try to print its value, it will not return any garbage value. After that, we have printed the value of the “argc” parameter on the terminal. Then, we have used a “for” loop for printing all the command line arguments on the terminal.

We have compiled this code using the command shown below:

$ g++ CommandLine.cpp –o CommandLine

Then, when we wanted to run this program, we did not pass any command-line arguments to it, as you can see from the following command:

$ ./CommandLine

From the output of this C++ program shown in the image below, you can see that no command-line arguments were passed to this function because of which the argument count was “0” and no arguments were printed on the terminal because the character array “argv” was also empty.

Running a C++ Program with Integer Type Command Line Arguments:

Now, we want to run the same C++ program by passing integer type command-line arguments to it. However, before doing that, we will slightly modify our code as shown in the following image:

The only modification that we made to this code is that we have removed the line “argc=0” from it because in this example, we want to print the actual number of the command line arguments passed to this program at run time. The rest of the code is exactly the same as used in the section above.

We recompiled our modified code using the command shown below:

$ g++ CommandLine.cpp –o CommandLine

Then, for executing this code, we used the following command-line arguments:

$ ./CommandLine 1 2 3

It means that we have passed three integer type command-line arguments to this C++ program while running it, i.e., 1, 2, and 3.

The output of this modified program is shown in the image below:

The total number of arguments returned by this C++ program is “4” i.e., three integer arguments that we have passed + the name of the object file. This program also printed the elements of the “argv” character array on the terminal, i.e., the actual integer type arguments that were passed to this program at the time of execution, along with the name of the program.

Running a C++ Program with Character Type Command Line Arguments:

Now, we wanted to see whether the same C++ program works fine when we try to execute it by passing character type command-line arguments to it. For that, we did not need to modify it any further. We only had to execute it with character type command-line arguments as follows:

$ ./CommandLine a b c d e f

It means that we have passed six-character type command-line arguments to this C++ program while running it, i.e., a, b, c, d, e, and f.

The output produced as a result of passing character type command-line arguments to the same C++ program is shown below:

The total number of arguments returned by this C++ program is “7” i.e., six-character arguments that we have passed + the name of the object file. This program also printed the elements of the “argv” character array on the terminal, i.e., the actual character type arguments that were passed to this program at the time of execution, along with the name of the program.

Conclusion:

This article was aimed at the discussion of the two command-line arguments, also known as the parameters of the “main()” function, i.e., “argc” and “argv”. We talked about the significance of these two parameters by stating their usage. Then, we shared a few examples with you that depicted the usage of “argc” and “argv” in C++ in Ubuntu 20.04. Moreover, we also clarified that even without using these parameters, the “main()” function could work perfectly well. Therefore, once you give this article a read, you will understand the usage of “argc” and “argv” very clearly in C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.