arguments for main ()

I sometimes see main () declared as
" int main ( int argc, char * argv ) {} "

I know what argc, and argv are. What I don't understand, though, is that if main () is where the program starts, then who ( or rather, what ) is gonna call "main"? In other words, why would one give main () some arguments?

P.S: I'm just a VERY beginner in C++!!

Last edited on
Those are the commandline arguments. If the program is run from the commandline, argv would point to strings containing the following commands.

ie:

if you run this from the command prompt:

 
myprogram.exe -option file.txt


Then you'd have the following:

1
2
3
4
argc == 3
argv[0] == "myprogram.exe"
argv[1] == "-option"
argv[2] == "file.txt"
It's used to give utility programs more instruction. or to get the current version (-v), author(-author), build date(-build), or other such things from the executable, also in the case of some programs on windows you can add extra flags to the windows shortcut so that the program starts up in taskbar, or starts in windowed mode (for games), or starts at a different size resolution to normal... The possibilities are endless...

also to be more precise it's
int main ( int argc, char **argv )
or more commonly written
int main ( int argc, char * argv[] )

Say you decide to unzip a program from command line, you might give it a couple options such as:

Unzip -v -f somezippedprog.zip

the -v (verbose) -f (force) and the filename (somezippedprog.zip) are 3 arguments that are sent to int main() and it handles these as required.
Last edited on
http://www.cplusplus.com/forum/windows/6264/

[edit]
Oh yeah, there is a library to help you manage command-line arguments of the kind gcampton is talking about:
GetOpt
http://www.gnu.org/s/libc/manual/html_node/Getopt.html

Hope this helps.
Last edited on
nice file association blog Duoas, i might need it later..

~thanks ^^
There is also a library in boost to handle Program Options
http://www.boost.org/doc/libs/1_42_0/doc/html/program_options.html
Topic archived. No new replies allowed.