Page 1 of 1

int main(int argc, char* argv[]) -- WTF?

Posted: Fri Sep 24, 2004 9:03 pm
by Falco Girgis
I don't want to be flamed; insulted; or be called called gay, the antichrist, etc.

I have never come upon a main function that looks like this:

Code: Select all

int main(int argc, char* argv[])
in all of my C++ book learning days. I've never seen that before. Now, it is being used in Dev-C++ default and same with VC++.

I alwasy see:

Code: Select all

int main()
Somebody want to tell me what the hell that stuff is?

int main(int argc, char* argv[]) -- WTF?

Posted: Sat Sep 25, 2004 8:49 am
by Don Pwnious
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 -



Code: Select all

for (int i=0; i<argc; i++)
  cout << argv[i];

This would cause the program to dump the variables to the terminal.

Posted: Sun Sep 26, 2004 12:56 am
by MarauderIIC
I thought I mentioned that to you. Maybe I only mentioned it to Phel.