Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views2 pages

Command Line Argument

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 2

Command Line Argument in C++

If any input value is passed through command prompt at the time of running of
program is known as command line argument. It is a concept to passing the arguments
to the main() function by using command prompt.
When Use Command Line Argument
When you need to developing an application for DOS operating system then in that
case command line arguments are used. DOS operating system is a command
interface operating system so by using command we execute the program. With the
help of command line arguments we can create our own commands.
In Command line arguments application main() function will takes two arguments
that is;
argc
argv
argc: argc is an integer type variable and it holds total number of arguments which is
passed into main function. It take Number of arguments in the command line
including program name.
argv[]: argv[] is a char* type variable, which holds actual arguments which is passed
to main function.
Compile and run CMD programs
Command line arguments are not compile and run like normal C++ programs, these
programs are compile and run on command prompt.
To Compile and Link Command Line Program we need TCC Command.
First open command prompt
Follow you directory where your code saved.
For compile -> C:/TC/BIN>TCC mycmd.cpp
For run -> C:/TC/BIN>mycmd 10 20
Explanation: Here mycmd is your program file name and TCC is a Command. In
"mycmd 10 20" statement we pass two arguments.
Example of Command Line Argument in C++
#include<iostream.h>
#include<conio.h>

void main(int argc, char* argv[])


{
int i;
clrscr();
cout<<"Total number of arguments: "<<argc;
for(i=0;i< argc;i++)
{
cout<<endl<< i;<<"argument: "<<argv[i];
getch();
}
}
Output
C:/TC/BIN>TCC mycmd.cpp
C:/TC/BIN>mycmd 10 20
Number of Arguments: 3
0 arguments c:/tc/bin/mycmd.exe
1 arguments: 10
2 arguments: 20

You might also like