WHAT IS A COMMAND-LINE ARGUMENT?

Can somebody explain to me in lay-man's terms what a command line argument is and its use in a C++ program. For example the statement int main(argc, argv*[]) is said to accept command-line arguments.
in the windows CMD you can enter :

c:\myprog.exe argument1 argument2


the 3 words (yes, 3!) are stored in argv.
And the number of arguments is stored in argc.
And you can access them like this

1
2
3
4
5
6
7
int main( argc , argv*[] )
{
  for(int i =0 ;i < argc ; i++)
  {
     cout << "argument " << i  << ": "<< argv[i] << endl;
  }
}


output:
argument 0: myprog.exe
argument 1: argument1
argument 2: argument2
Last edited on
Do you ever use the command line or just GUI apps?
But why would you want to enter this command c:\myprog.exe argument1 argument2 in the Windows CMD. Please give me an example. I understand what you are saying, I just don't understand the purpose of it.
Here is how to use the command line to rename a file.

mv inputFileName newFileName


The program is
mv
By passing in the arguments
inputFileName  
and
newFileName
we tell the program which file to rename, and what the new name of the file should be.

Is that example clear enough?
Last edited on
I've used command-line when working with IP protocols like ipConfig, ping etc.
So when you used ping, presumably you typed something like
ping 134.254.32.43


Hey look, a command line argument!
Last edited on
so I've written this short program below. Does this mean now I can open up the command prompt and type in the program's name and maybe 5 and 6 as arguments and the program will run?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    int x, y;
    
    x = static_cast<int> (*argv[1]);
    y = static_cast<int> (*argv[2]);
    
    cout << x <<" Multiplied by "<< y <<" is equal to " << x*y <<endl <<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}
The way you're converting a char to an int isn't going to work how you think it is, but otherwise yes.
I think I understand now. One last question: in the CMD prompt, do you have to specify the path to your .exe or can you just enter the name of your .exe without specifying the path as you would do with "ping" and "ipconfig"?
It depends. If you're in the directory of your exe, you don't need to specify the path.

Furthermore, there's an environment variable named PATH, which can be altered. When you run a command, any directories listed in the PATH variable will be searched to see if the command/exe you're trying to run exists in any of them.

You can see what's currently in there by typing echo %PATH% in the command prompt.

The easiest way to add something to the path is to do so in the Environment Variables... section of the advanced system settings. If you add a directory to the path, you won't need to explicitly type the path when trying to run a program stored within it.
Last edited on
Thank you very much to everybody who replied. Now I can safely say, I understand command-line arguments.
Last edited on
Topic archived. No new replies allowed.