This question already has answers here:
Closed 11 years ago.
Duplicate:
What is the proper declaration of main?
What do we mean by the arguments in this main function? What are they trying to tell us?
int main(int argc, char** argv)
UPDATE: And, is the preceding line of code similar to this int main(int argc, char* argv[])? If so, how can we say that char** argv is similar to char* argv[] as they don't look similar from an array point of view?
How is it compared with int main() which does not have any arguments?
Thanks.
The argc parameter is the number of command line options specified, including the executable name, when the executable was invoked. The individual command line options are found in the argv array, which is NULL terminated (the name and path used to invoke the executable is in argv[0]).
The difference between the two versions is simply if you want to parse command line arguments or not - if you are not interested in them then you can ignore them using the second form.
Wikipedia offers a good explanation. The first parameter gives you the number of command line arguments, and the second gives you the actual arguments.
They represent the command line parameters.
argc is the number of command line parameters, including the name of the executable.
argv is an array of null-terminated strings, where argv[0] is the command line parameter, and argv[i] is the ith parameter after that, argv[argc-1] being the last one and argv[argc] is actually well defined and a NULL pointer.
Thus:
foo bar baz
on the command line will have argc=3, argv[0]="foo" argv[1]="bar" argv[2]="baz" argv[3] = NULL
Note that there is no special attachment placed for "flag" arguments.
grep -i foo bar.cpp bar.h
would have 4 arguments (argc=5 including grep itself), -i being one of them and this would apply even if the next parameter was a "value" attached to the flag.
Note if you did a wildcard
grep -i foo *
in UNIX at least, the * would be expanded before the call into grep and thus each file that matched would be an argument.
Incidentally
char** argv and char* argv[]
do the same thing.
Also while the standard says you must use one of these signatures (you shouldn't even add in any consts) there is no law you have to use those two variable names, but it is so conventional now that they are pretty much universal. (i.e. you could use argCount and argValues if you want).
argc gives you the number of arguments and argv gives you those arguments. The first one is the path to the .exe used to run your program, the following ones are arguments the caller of your .exe provided on the command line like this:
my.exe arg1 arg2
Whereas
int main() {}
just ignores the arguments.
argv is an array holding the commandline parameters passed to the application. argc tells you the number of elements contained in that array.
Related
Everytime I create a project (standard command line utility) with Xcode, my
main function starts out looking like this:
int main(int argc, const char * argv[])
What's all this in the parenthesis? Why use this rather than just
int main()?
main receives the number of arguments and the arguments passed to it when you start the program, so you can access it.
argc contains the number of arguments, argv contains pointers to the arguments.
argv[argc] is always a NULL pointer. The arguments usually include the program name itself.
Typically if you run your program like ./myprogram
argc is 1;
argv[0] is the string "./myprogram"
argv[1] is a NULL pointer
If you run your program like ./myprogram /tmp/somefile
argc is 2;
argv[0] is the string "./myprogram"
argv[1] is the string "/tmp/somefile"
argv[2] is a NULL pointer
Although not covered by standards, on Windows and most flavours of Unix and Linux, main can have up to three arguments:
int main(int argc, char *argv[], char *envp[])
The last one is similar to argv (which is an array of strings, as described in other answers, specifying arguments to the program passed on the command line.)
But it contains the environment variables, e.g. PATH or anything else you set in your OS shell. It is null terminated so there is no need to provide a count argument.
These are for using the arguments from the command line -
argc contains the number of arguments on
the command line (including the program name), and argv is the list of
actual arguments (represented as character strings).
These are used to pass command line paramters.
For ex: if you want to pass a file name to your process from outside then
myExe.exe "filename.txt"
the command line "filename.txt" will be stored in argv[], and the number of command line parameter ( the count) will be stored in argc.
main() is a function which actually can take maximum three parameters or no parameters.
The following are the parameters that main() can take is as follows:-
1) int argc : It holds the number of arguments passed (from the command prompt) during the execution of program or you can say it is used ot keep a track of the number of variables passed during the execution of program. It cannot hold the negative value. Eg. If you pass your executable file "./a.out" then that will be considered as a parameter and hence argc value will be 0 i.e 1 value is passed.
2) char *argv[] : it is an array of character pointers which holds the address of the strings(array of characters) that are passed from the command prompt during execution of program. Eg. in above example if you wrote argv[argc] i.e argv[0] in cout then it will give ./a.out as output.
3) char*envp[] : it is also an array of character pointer which is used to hold the address of the environments variables being used in the program. Environment variables are the set of dynamic named value that can affect the way the running process will behave on the computer. For example running process want to store temporary files then it will invoke TEMP environment variables to get a suitable location.
This question already has answers here:
Creating files in C++
(8 answers)
Closed 8 years ago.
I have a path given in command line with a filename in it like:
./something.bin infomration /some/some2/thing.txt
I know I can put the /some/some2/thing.txt into a global variable and use it. Now, I want to:
create a file named thing.txt in the path /some/some2/
For things like this you'll have to use the parameters passed to your main entry point:
int main(int argc, char **argv) {
// some other code
return 0;
}
As you can see, main() will receive two parameters:
argc: The first parameter will include the number of parameters passed through the second parameter.
argv: The second parameter points to pointers that point to strings with the actual parameters.
The usage is quite trivial, but it's important to remember that the very first "argument" is the executable file itself.
In your example, argc would be set to 3 and argv would point to the following strings:
argv[0]: ./something.bin (based on the OS, this could also be an absolute path)
argv[1]: infomration [sic]
argv[2]: /some/some2/thing.txt
As you can see, all you'll have to do is the following:
Verify the number of arguments is correct.
Verify the second argument (infomration) is correct.
Use the third argument to receive the actual file name.
Keep in mind that the user might pass invalid parameters (e.g. invalid characters for a file name) or he might try to trick you into doing something you shouldn't do (like passing reserved names).
This question already has answers here:
Reading command line parameters
(4 answers)
Closed 9 years ago.
When you right click on a program in windows(such as starcraft.exe) and look at its properties there is a text field called "target" which contains the full path of the binary. I have seen programs able to parse flags added to the target such as "C:\programfiles\myprogram\myprogram.exe -x 1280 -y 360" and the program would start up in the specified resolution. My question is how to read those arguments, if it is done by argv[] please just inform me of my stupidity.
C++ is the language, VS express 2012 desktop is the environment.
you receive those parameters when calling executable main method int main(int argc, char* argv[]) as argc (count) and argv[] parameters all you have to do is just parse them
here is an example How to parse command line parameters
You'll want to avoid comparisons between char* and string literals without strncmp.
Remember that argc is the the argument count (including the program name).
argv is an array of C strings specifying the arguments (first being the program name as invoked).
In this case, you're usually best off using a library, like getopt. This will make interleaved options, long options, and arguments much more sane to manage (assuming order between options and arguments is largely unimportant).
I am having trouble using getopt_long() function with custom argc and argv.
I receive my arguments in a string instead of the real command line args. Then a new_argc and new_argv was built from this string to be used with getopt_long(). But getopt_long() fails on the first call itself. returns EOF and optarg = NULL.
string is "-c 10.30.99.41"
new_argc = 3
new_argv[0]=>./prog_name
new_argv[1]=>-c
new_argv[2]=>10.30.99.41
getopt_long works OK for me if I pass command line args. So my short and long options are correct. But if I pass the new_argc and new_argv it fails.
I am sure my short and long options are right and the argv is NULL terminated. I apologize I cant post more code here.
I doubt if getopt_long can be used with a custom argc and argv. I suspect it works only with a real argc and argv because it must be referencing some other code in libc related to argc,argv. Any comments?
option = getopt_long( new_argc, new_argv, short_options, long_options, NULL );
EDIT:
"The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. The caller can reset it to 1 to restart scanning of the same argv, or when scanning a new argument vector."
So, yes. You can use getopt_long to scan the arguments or another argument list again. However, if someone has called getopt_long previously, you have to set the global optind variable to back to 1.
Remember that the argv in main() is NULL terminated and argc long, that is; argv[argc] == NULL. So you likely have to make sure the last element in your own new_argv is a NULL pointer.
(Note, please show all the relevant code when posting, it's hard to guess what the error is, e.g. showing what short_options, long_option is, how you actually build your new_argv, variable declarations etc.)
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.
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 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?
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.