Ad Code

Why we Pass Command Line Argument ?

Why we pass command line arguments to our main function.If we do not pass what will happen? For which type of program these are important.


Command line arguments is a programming concept and you have studied it in CS201 course, however your program does not affect if it does not use command line arguments, therefore when you change the signature of main function and the program did not affect.

Here is a little more explanation and use of command line arguments.

A program's main() function is passed two parameters. The first is an integer count, the number of arguments on the command line while the program name is also counted as an argument, so every program has at least one parameter.

The second parameter passed to main() is an array of pointers to character strings.

In order to see how many arguments have passed to a program from command line, consider the following simple example (Use of Command Line Arguments),

#include <iostream.h>
#include <conio.h>

 int main(int argc, char **argv)
    {
       cout << "Received " << argc << " Arguments"<<endl;    
        for (int i=0; i<argc; i++)
        {
           cout << "Argument " << i << ": " << argv[i] << endl;
        } 
               getche();
               return 0;
}
Reactions

Post a Comment

0 Comments