Passing Arguments to main( ) function

In c++ , you can accept arguments from command line.

For this you need to pass arguments to main function. here, argC means Agument Counter that counts number of arguments you passed from command line  and argV means Argument Vector means values you passed. argC is always 1+ number of arguments you passed with the executable file. Example: I f you passed 3 arguments with a.out file as ( ./a.out ab cd ef ), then it will count it as 4 argument considering ./a.out as a first argument. Note:  Value at index 0 of array (i.e argV [0] )  is always the command( that may include path as /home/jasleen/./a.out )

Program:

#include<iostream>
using namespace std;
int main(int argC,char *argV[])
{
    cout<<"There are "<<argC<<" argumentsn";
    for(int i=0;i<=argC;i++)
    {
        cout<<argV[i]<<"n";
    }
}
Advertisements

About Jasleen Kaur

Hi, I am Jasleen kaur, a Computer Science Student.
This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s