17

I've known how to write a program that accepts command line arguments ever since I learned to program. What I don't understand is how these parameters get their values. Hopefully I don't have these two mixed up but there is a difference between an argument and a parameter. An argument is the value given to the function when it is called such as: foo( a, b, c); where a, b, and c are the values. A parameter is the values that are inside the function while is being called.

So my question is how does a person pass command line arguments to a program? I understand how to read the arguments, that argc is the number of arguments, argv is a pointer to an array of strings containing the arguments, etc. etc. but I just don't know how to give those arguments a value..

I'm looking for information for both C and C++. I'm sort of a novice at this.

  • 2
    This would be operating system specific, though on a unix-style system you can look at the man pages for the exec() family of functions - that is what a command shell normally uses with the arguments which it has parsed off the command line. – Chris Stratton Jul 15 '13 at 01:18
  • "I've never used this functionality because I don't know how to pass such arguments to the program" - two sentences earlier: "I've known how to write a program that accepts command line arguments ever since I learned to program" - now **what is the truth?** Also, it doesn't seem to me you have gone through Chapter 1 of a beginner's C++ tutorial, this would have been described quite early in there. –  Jul 15 '13 at 05:24
  • That is the truth. I don't think it came across very clear though. I want to know how to pass an argument to a program, not a parameter! I wish to know how the values are given to the program as arguments to be used as parameters! In other words how does the user give the program its arguments? As Yu Hao, TGH, and Nobilis had what I was looking for. –  Jul 16 '13 at 07:31
  • 1
    @user529758 I guess I ought to clarify (three years later). I did not know how to pass a command line argument to a program at all when I posted this question. Regardless, whatever classes or books I read never elaborated on how to do that. Everything I did was in an IDE like VS Express. And Chris Stratton I can guarantee I didn't know what a manpage was or an exec() system call was back then either. I know these things now though! :) I don't think I had ever used Linux at the time. –  May 31 '16 at 12:59
  • 1
    Possible duplicate of [What does int argc, char \*argv\[\] mean?](https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean) – jww Apr 26 '18 at 00:01

4 Answers4

13

In a Windows environment you just pass them on the command line like so:

myProgram.exe arg1 arg2 arg3

argv[1] contain arg1 etc

The main function would be the following:

int main (int argc, char *argv[])
TGH
  • 38,769
  • 12
  • 102
  • 135
  • 1
    To add to this, your `main` function declaration would be `int main (int argc, char *argv[])`, and you should be able to get `argv[1]`, `argv[2]` and so on. – vee Jul 15 '13 at 01:20
10

On *nix:

$ ./my_prog arg1 arg2

On Windows command line:

C:\>my_prog.exe arg1 arg2

In both cases, given the main is declared as:

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

argc will be an int with a value of 3, argv[1] = "arg1", argv[2] = "arg2", additionally, argv[0] will have the name of the program, my_prog.

Command line arguments are normally separated by space, if you wish to pass an argument with a space, like hello world, use a double quote:

$ ./my_prog "hello world"
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

On *nix, there's a very nice utility that lets you parse command-line flags and arguments in a very straightforward way. There's a nice example of its use on the same page.

You would then run your program and pass arguments to it in a very standardised way:

$ ./my_app -a -b -c argument1 argument2

You can do without it and just parse them on your own but if you're aiming to make your app useful to other people it's definitely worth the effort of making it conforming.

Community
  • 1
  • 1
Nobilis
  • 7,310
  • 1
  • 33
  • 67
1

Just click on start menu and type cmd in search index...press enter ..now in cmd window type following command... "program_name arg1 arg2" (without quotes) and press enter key...and yeah its done! and

  • Please try and add some new lines to your answer, it is slightly hard to read here, you may also want to note this is Windows only. However +1 as you are correct. – Vality Aug 19 '14 at 10:39