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?
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
.
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."
Mar 7, 2011 at 8:39
argv[ argc ]
is '0'
(§3.6.1/2 [...]The value of argv[argc] shall be 0.)
Mar 7, 2011 at 8:42
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. ;)
Mar 7, 2011 at 9:03
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
.
argv[0] is the execution path of the program, argv[1] is the first parameter to the program
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 fileargv[1]
is the 1st argumentEdit:
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?
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).
Mar 7, 2011 at 8:29
argc == 1
then argv[1] == 0
. §3.6.1/2 explicitly states that argv[argc] == 0
Mar 7, 2011 at 8:44
Short answer is yes, the array contains all the options passed into the program.
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
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.