I have never come upon a main function that looks like this:
Code: Select all
int main(int argc, char* argv[])
I alwasy see:
Code: Select all
int main()
Moderator: Coders of Rage
Code: Select all
int main(int argc, char* argv[])
Code: Select all
int main()
kode monkey wrote:As I'm sure you already know the function main is the entry point into your program. The int before it shows that it returns an integer. Normally this would return to a function in your program but the return from main goes to the operating system. Generally main should just return 0 which means it exited successfully but you can return other codes for other situation.
The two arguments it takes int argc and char *argv[] refer to the commandline arguments specified to the program. argc holds the number of arguments (including the program's name) and argv holds the arguments as an array of arrays of characters. (A list of strings).
So if you were to run the program foo with the arguments "-a bar 2" then argc would be equal to 4 (3 arguments and the program name) and argv would contain ["foo", "-a", "bar", "2"] so you could do something like -
This would cause the program to dump the variables to the terminal.Code: Select all
for (int i=0; i<argc; i++) cout << argv[i];