0% found this document useful (0 votes)
42 views2 pages

Command Line Argument

Command line arguments in C++ allow passing input values to a program's main() function when running the program from the command prompt. The main() function takes two arguments: argc, which is the number of arguments including the program name, and argv, which is an array of character strings containing the actual arguments. Programs using command line arguments are compiled and run from the command prompt using compiler commands like TCC, and arguments can be passed during runtime by listing them after the program file name.

Uploaded by

sridharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Command Line Argument

Command line arguments in C++ allow passing input values to a program's main() function when running the program from the command prompt. The main() function takes two arguments: argc, which is the number of arguments including the program name, and argv, which is an array of character strings containing the actual arguments. Programs using command line arguments are compiled and run from the command prompt using compiler commands like TCC, and arguments can be passed during runtime by listing them after the program file name.

Uploaded by

sridharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
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