| by Arround The Web | No comments

Argc and Argv in C++

We learned a lot about how to pass parameters in functions previously. We further discovered why parameters are passed and what we get in return from functions. However, we often did not supply any arguments or parameters to the main() function. In C++ programs, we can also pass the parameters in the main() function. These parameters are called command line arguments or command line parameters. We pass the parameters while running the program on the command prompt window just after the program’s name. These two parameters are “argc” and “argv”. The “argc” stands for argument count and it is used to store the non-negative integer type argument including the program name. The argv stands for argument vector because it is a character pointer of an array that stores the actual parameters.

Syntax:

This is the syntax of passing the command line arguments in C++ programming language. Since it is a command line, argument integration rule permits us to pass the parameters in the program’s main() method without encountering any kind of error in the code.  The name of the function, “main,” will be written first. Next, we will pass the integer type “argc” argument and character type “argv” array in the main() function small brackets.


Let us get into the command line argument examples and properly understand how well these parameters will behave when we pass them to the main() function.

Example 01:

Let us use a real-world example of a command line argument to better understand how to use command line arguments in the C++ programming language. We utilized a Dev-C++ translator to construct a command line parameter in C++, which converts the code into computer language.  Before using any functions in the program, we first integrate the package for the predefined function since the cin() and cout() methods are the basic foundation of any program that is used to collect information from users and represent it in a user’s console window.

For that, we have included the “#include <iostream>” header file so that we may use those methods in the existing program. Then, to prevent assigning the same name throughout the contexts in the current program, we must use the “using namespace std” declaration. If we did not write the namespace declaration before the main() function, then we have to write the “std” with every built-in method of C++ language.

Now, let us develop the main() function after incorporating the namespace and header file in the program. As a way to implement the idea of command line arguments, we will first write the function’s return type, which is an integer type, followed by the function name, “main,” and last by passing the parameters in the function brackets. The first argument we passed is “argc” of integer type so that we can count the number of parameters that we are going to insert during the run-time of the program. Next, the parameter is “argv” of the character type so that if we entered the value of the argument in character, it would accept that value and also show the index by using a “*” pointer with the “argv” argument.

#include <iostream>
using namespace std;  
int main(int argc, char** argv)
{
    cout << "*-----Implementation of Command Line Arguments in C++-----* \n";
    cout <<"Program Name is: " << argv[0] << endl;
    cout << "The Total Number of Arguments are: " << argc <<endl;
       
    for (int i = 0; i < argc; ++i)
    {
        cout <<"The Argument Value at ["<<i<<"] is: " << argv[i] << endl;
    }
    return 0;
}

 
To write the code in the main() function body that we want to implement, open the main () function left brace “(“. We want to display the message to the output screen by using the cout() method so that the user can understand what we are implementing in the program. To display the name of the example that we created, we have passed the “argv[]” array in the cout() statement.

The “endl” is used at the end of the cout() statement so that the compiler will display the result in the new line. We have supplied the “argc” option to the program to determine the number of arguments. To display the index of each argument in the program, we will use the “for loop”. In the for loop body, we will pass the “argv” argument to display the arguments one by one in a new line. We shall return 0 to the main() method after the program’s implementation so that the translator will halt the program’s execution.

To display the output of the above-implemented code, we will open the command prompt of windows by writing “cmd” in the main menu search bar. The command prompt window will be like this:


Now, we will write the path of the file where this program file is saved in the windows command prompt. As you see below the example file path:


To enter the CPP folder where the project1 is saved, we will write the following command and then we will enter into the CPP folder:


Now, we can access the results of the program by writing the program name which is “project1”:


Case 01: In this output, we did not enter any arguments. But here, it will show us that there is one argument because in the command line argument, the first argument will always be the name of the program and here it is “project1”:


Case 02: To insert values in the program, we again first write the program name “project1” and then we insert a string, which you can see below in the command prompt window. First, it displays the title of the program which tells the user we have implemented the command line argument technique. Next, it displays the program name, and then we have a total number of arguments. And then it displays the arguments one by one with their index value.

Conclusion

In this tutorial, we have studied what are command line arguments in C++ programming language. We have briefly discussed the use of command-line arguments and we have also learned the writing rules. We have run an instance that includes a detailed description of each and every line of code. We have also learned the way of accessing the output of the command line argument in C++ language.

Share Button

Source: linuxhint.com

Leave a Reply