4

If I have this:

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

In the body, you can sometimes find programs using argv[1].

When do we use argv[1] over argv[0]? Is it only when we just want to read the second argument in the command line?

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Simplicity
  • 47,404
  • 98
  • 256
  • 385

7 Answers7

12

By convention, argv[0] is the current program's name (or path), and argv[1] through argv[argc - 1] are the command-line arguments that the user provides.

However, this doesn't have to be true -- programs can OS-specific functions to bypass this requirement, and this happens often enough that you should be aware of it. (I'm not sure if there's much you can do even if you're aware of it, though...)

Example:

gcc -O3 -o temp.o "My file.c"

would (should) produce the following arguments:

argc: 5
argv: ["gcc", "-O3", "-o", "temp.o", "My file.c"]

So saying argv[0] would refer to gcc, not to -O3.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Well, argv[0] doesn't have to be the real path to the executable, but argv[1]..argv[argc - 1] have to be the command line arguments :-). – Tony Delroy Mar 07 '11 at 08:29
  • 6
    5.1.2.2.1 section 2 point 4: "If the value of `argc` is greater than zero, the string pointed to by `argv[0]` represents the program name; `argv[0][0]` shall be the null character if the program name is not available from the host environment. If the value of `argc` is greater than one, the strings pointed to by `argv[1]` through `argv[argc-1]` represent the program parameters." – Chris Lutz Mar 07 '11 at 08:39
  • @Chris: That's what we *hope* to be true, but you can easily prove it to be false on Windows in some situations. :\ – user541686 Mar 07 '11 at 08:42
  • 1
    +1, For completion also by convention (that is, because the standard says so) `argv[ argc ]` is `'0'` (*§3.6.1/2 [...]The value of argv[argc] shall be 0.*) – David Rodríguez - dribeas Mar 07 '11 at 08:42
  • @Mehrdad - MSVC has never pretended to be a conformant implementation of C. @David - No, `argv[argc]` is of type `char *`, not `char`, so the value of `0` is interpreted as `NULL`. – Chris Lutz Mar 07 '11 at 08:44
  • @Chris: Yeah, that's why we have to be aware of it. ;) – user541686 Mar 07 '11 at 08:44
  • @Chris, you are right, that is what happens when I type without thinking :) (I've cheated and corrected the previous comment, for those that read this after the fact) – David Rodríguez - dribeas Mar 07 '11 at 08:45
  • @Mehrdad - My point is, if it's in the standard it's a little more than convention. – Chris Lutz Mar 07 '11 at 08:46
  • 3
    @Chris: UNIX/Linux too of course: all the exec famility of functions allow the executing process to specify an argv value for its child - by convention that's the child's executable name, but there's a well known decades-old hack to hide that you were running e.g. irc and appear to be running say vim: simply have a tiny program exec the former with the latter forced into argv[0]. That way a simple ps etc. will make it look like you're working. – Tony Delroy Mar 07 '11 at 08:50
  • @Chris: "if it's in the standard it's a little more than convention" - still a matter of definition... the Stanard's requirement is met if "program name" is defined as whatever the executing function decides it should be. I can't see any particular stipulations about that in the Standard... do you know of any? – Tony Delroy Mar 07 '11 at 08:53
  • 2
    @Tony - If someone does an `exec` to trick a program into thinking its name is something else, then we should expect that program to think that its name is whatever we told it to be, shouldn't we? Therefore, a program should trust that `argv[0]` is its name, and if we've lied to our child program we should feel appropriately ashamed of ourselves. ;) – Chris Lutz Mar 07 '11 at 09:03
6

argv is an array of pointers, and each pointer in this array stores one argument from command line. So argv[0] is the first argument (that is the executable/program itself), argv[1] is the second argument, and so on!

The total number of arguments is determined by argc.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Why the -1: This is exactly correct. argv[1] is the second argument on the command line as interpreted by the shell. And is the first argument that should be interpreted by the application. – Martin York Mar 07 '11 at 08:32
  • @Nawaz - Someone probably leapt before they looked. Have an upvote and be at peace knowing this answer gave you a net gain of 28 reputation. :) – Chris Lutz Mar 07 '11 at 09:12
  • @Nawaz, @Martin: "each pointer in this array stores the *argument* from the command line". I believe this is misleading, and that the first value - the program name - should not be considered a command line *argument*; it is simply the program name. (It's the exact same issue that led to 4 people questioning the now-deleted answer from Alnitak.) I couldn't find anything in the Standard supporting either perspective... any authoritative reference and I'll reconsider, but I currently believe this is wrong. – Tony Delroy Mar 07 '11 at 09:22
  • @Nawaz, @Martin: for lack of a better reference, I'll accept the Linux man page's usage which speaks of "an array of pointers...represent the argument list available to the new program. The first argument, by convention, ...". I stand corrected, but can't reverse my down-vote unless the answer is edited. Nawaz: sorry about that! – Tony Delroy Mar 07 '11 at 09:30
  • @Tony: Which part in my post should I edit? Also, I don't think that is misleading IF you read the *complete* post. – Nawaz Mar 07 '11 at 09:33
  • @Tony: Alright, I just made it "*one* argument from command line" from "*the* argument from command line". – Nawaz Mar 07 '11 at 09:35
  • @Nawaz: one vs the or a is a minor but worthwhile improvement (I wasn't going to mention it myself). Otherwise, the still-slightly-doubtful bit in my mind (even accepting the Linux manpage's usage) is whether there's distinction between your "argument *from the command line*" and being part of the argument list. You might consider replacing command line with argument list, or adding a quote to support the idea of the argument list including the program name, but it's your call.... – Tony Delroy Mar 07 '11 at 09:44
  • @Tony: How does that matter as long as you understand that `argv[0]` is the executable itself? All you're discussing is the terminologies, not the concept or how the program works! – Nawaz Mar 07 '11 at 09:46
  • @Nawaz: it's important because we're trying to communicate unambiguously how the program works, and if we don't have universally understood terminology then the statements we make about the way things work can be understood by readers to imply different concepts and behaviours than intended. (If you relied less on the terminology and showed how individual values mapped onto the argv[] content - as Oscar and Mehrdad do in their answers - there'd be less room for confusion.) – Tony Delroy Mar 07 '11 at 09:58
  • @Tony: Did you read my post COMPLETELY? Especially the line `argv[0] is the first argument (that is the executable/program itself)`? – Nawaz Mar 07 '11 at 10:08
  • @Nawaz: yes Nawaz, and I disagreed before because I've considered the first argument "from the command line" (context carried over from the previous sentence") to be argv[1]. I've since given you the benefit of any doubt because the linux `exec` man page considers the "argument list" to include the program name and I can't find a more reputable source specifying whether the program name is considered an argument or not. Anyway, I've gone from downvoting to upvoting you - what more do you want...? ;-P – Tony Delroy Mar 07 '11 at 15:16
  • @Tony: All I'm saying is that it doesn't really matter what you call it. All matters is *what you mean* by what you call. Now I'm going to call it neither "command line", nor "argument list", I call it abracadabra-line-list, and by it I mean what I previously meant "command line", or what you mean "argument list". Hope you got what I mean now :D – Nawaz Mar 07 '11 at 15:26
2

argv[0] is the execution path of the program, argv[1] is the first parameter to the program

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
2

Let's suppose your C++ executable file is:

/home/user/program (or C:\program.exe in Windows)

if you execute:

./home/user/program 1 2 (or C:\program.exe 1 2 in Windows)

argv[0] = /home/user/program (C:\program.exe)
argv[1] = 1
argv[2] = 2

That is because:

  • argv[0] is the path of the executable file
  • argv[1] is the 1st argument

Edit:

Now I see that argv[0] isn't necessarily the path of the executable file.
Read the following SO question: Is args[0] guaranteed to be the path of execution?

Community
  • 1
  • 1
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
  • 1
    Note that if `argc == 1` then `argv[1]` doesn't exist. `argc` is not the number of arguments, it is the size of `argv` (1 more than the number of arguments). – Chris Lutz Mar 07 '11 at 08:29
  • 1
    @Cris Lutz: Rather, if `argc == 1` then `argv[1] == 0`. §3.6.1/2 explicitly states that `argv[argc] == 0` – David Rodríguez - dribeas Mar 07 '11 at 08:44
  • @David - I didn't actually know that until I looked it up for Mehrdad's answer. Glad to know I wasn't going to get away with that. – Chris Lutz Mar 07 '11 at 08:47
0

Yes, that's mostly it, argv[1] is the second command line parameter. The first command line parameter is the name of the program itself.

Alternatively, to avoid the semantic mess that this answer originally had, and the comments from others, it might make sense to call argv[0] the zeroth parameter, so that argv[1] would now be the "first" of the user supplied values.

In any event, this comes from the exec() family of functions, e.g. execl which has usage:

 int execl(const char *path, const char *arg0, ... /*, (char *)0 */);

In the (Unix) shell when you type in a command, if necessary the shell first resolves the command name (using $PATH) to find the real absolute path. The (absolute or relative) path is supplied for path, and the command as originally typed-in is supplied as arg0, eventually becoming argv[0] in your program.

The remaining command line parameters then end up as argv[1], etc.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 3
    Huh? I'm not sure what your definition of "argument" is here, but AFAIK it's the first argument, not the second... – user541686 Mar 07 '11 at 08:26
0

Short answer is yes, the array contains all the options passed into the program.

Nim
  • 33,299
  • 2
  • 62
  • 101
0

as argv[0] is filepath of the program itself. Extra command line parameters are in further indexes, argv[1],argv[2].. You can read more here : http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175