Command Line Arguments in C++

In c++, if any input value is passed through command prompt at the interval of running of program is recognized as command line argument. It is a thought to passing the arguments to the main () function by means of command prompt.

When you want to developing an application for DOS operating system then in that case command line arguments are cast-off. DOS operating system is a command interface operating system so by using command we implement the program. With the help of command line arguments we can make our own commands.

In Command line arguments application main () function will proceeds two arguments that is;

argc: argc is an integer type variable and it embraces total number of arguments which is passed into main function. It take Number of arguments in the command line comprising program name.

argv[]: argv[] is a char* type variable, which clutches actual arguments which is passed to main function.

Compile also run CMD programs

Command line arguments are not compile also run like normal C++ programs, these programs are compile then run on command prompt. To Compile and Link Command Line Program we need TCC Command.

  • First of all, open command prompt
  • Follow you directory where your code set aside.
  • For to compile -> C:/TC/BIN>TCC mycmd.cpp
  • For torun -> C:/TC/BIN>mycmd 10 20

 Here mycmd is your program file name besides TCC is a Command. In “mycmd 10 20” statement we pass here two arguments.

Example :

Output:

Here, In above output we passed two arguments nevertheless is spectacle “Number of Arguments: 3” as argc take Number of arguments in the command line comprehending program name. Thus here two arguments plus one program name (mycmd.exe) total 3 arguments.

Example of Command line argument

Output:

the command line arguments program not as the crow flies run form TC IDE

Command line arguments connected programs are not implement directly from TC IDE since arguments can not be passed.

To Be in charge of the Command Line Argument Program use edit Command.

Syntax

C:/cprogram>edit mycmd.cpp
  • At whatever time the program is compiled also link we will get .exe file and that .exe file that one is command.
  • Here,In program name is mycmd.cpp so executable file is mycmd.exe as well ascommand name is mycmd.
  • To load the application in to the memory we are obligatory to use program name and command name.

To Access data from outside of main

  • argc and agrv are local variables to main function as those are the main function parameters.
  • Agreeing to the storage classes of C argc and argv are auto variable to main function, so we can notcover the range of auto variable.
  • By using argc and argv we can not access command from data external of the main function.
  • In setting into practice when we needed to access command from data outside of the main function then use _argc, _argv variables.
  • _argc and _argv are global variable which is acknowledged in dos.h.

Example:

Output:

 

 

Leave A Comment

Your email address will not be published. Required fields are marked *