Command line Arguments In C++

You know that functions can have arguments. You also know that main ( ) is a function. In this section we’ll take a look at how to pass arguments to the main function. We are dealing with this topic now because what you pass will usually be filenames.
First of all, let us suppose that we have a file by the name marks.cpp. From this file we make an exe file called marks.exe. This is an executable file and you can run it from the command prompt.
The command prompt specifies what drive and directory you are currently in. The command prompt can be seen in the ms-dos prompt.
C:\WINDOWS> This denotes that you are in C drive and in the directory of Windows.
Your marks.exe program is in this directory(let us assume this). To run the program you will type:
C:\WINDOWS> marks name result
You must be thinking that we will type only the name of the program? In this case the C++ program that you wrote is assumed to have arguments for the main function as follows:
int main (int argc, char * argv[ ] )
argc (the first argument-argument counter) stands for the number of arguments passed from the command line.
argv (argument vector) is an array of character type that points to the command line arguments.
In our example, the value of argc is 3 (marks, name, result). Hence for argv we have an array of 3 elements. They are:
argv[0] which is marks
argv[1] which is name
argv[2] which is result
Note: argv[0] will be the name that invokes the program (i.e. it is the name of the program that you have written).
If you feel a little vague in this section don’t worry. In the next section we’ll take a look at a simple program.
A program using Command Line Arguments
We’ll take a look at a program to demonstrate command line arguments. We shall start writing our programs in the proper C++ style with the new headers.
In case the new style headers don’t work in your compiler then you can use the old style.
The green colour indicates what you don’t have to type (like the dos prompt). We’ve used green for the output as well. Red denotes (as usual) the code of the program as well as what you have to type.
Out put of the program
Save the file as test.cpp. Compile it and then make the executable file (test.exe). If you run test.exefrom Windows (i.e. by just double clicking on the file), the output may just be as follows:
The value of argument counter is 1
This will be the output since you didn’t specify the arguments. To add the arguments you have to go to DOS prompt. From there type:
c:\windows>test one two three
You have to go to the folder in which you have the test.exe file. From that folder type the above line.
The output would be as follows:
The value of argument counter (argc) is 4
Hope you understood how the command line arguments work.
Objects being passed as Arguments
Remember we discussed about pass by value and pass by reference methods in functions.
If we don’t specify pass by reference then the compiler will actually work on a copy of the original argument (and not directly on the argument itself).
So, what happens when we pass objects? We saw the friend function earlier that has objects as it’s arguments.
Does the function create a duplicate object and work on the new object or does it work on the real object that you passed? Check out the same program below:
Out put of the program
The first two lines appear because we create two objects: fever and cholera.
The 3rd and 4th lines appear because that is what we ask the check ( ) function to do
In lines 5 to 8, we see that 2 bacteria objects and 2 virus objects are being destroyed.
From where did the program create 2 bacteria and 2 viruses??? The answer lies in the fact that the function acts like the pass by value method.
The function check ( ) creates a copy of the original arguments and works on these new objects that it created. But then why hasn’t the constructor been invoked two extra times?
Remember that the function has to work on the exact copy of the original object. Constructors are usually used to initialize the member data.
Thus, if the function invokes the constructors (it will initialize the member data), it will not be able to act on exact replicas of the original object. So, it does not invoke the constructor.
But whatever has come into existence has to be destroyed and thus the 2 extra organisms are destroyed. There is a very important fact to be noted here.
Usually we will specify the ‘delete’ keyword in the destructor to free up any memory that was taken up from the heap.
When a function creates a copy of an object, it will copy the destructor also exactly as it is in the original object.
Thus in the original object you will have a statement using ‘delete’, which will free up some particular memory space. The copied object will also have the same destructor and thus it will also ‘delete’ from the same memory area.
You should never use the delete twice (We mean if you have one ‘new’, you should have only one ‘delete’).
Double ‘delete’ can lead to serious errors. Thus the method of copying objects can sometimes lead to problems and for this purpose we have copy constructors (We’ll deal with them later).
Scroll to Top