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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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?
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

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

Post 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.
Last edited by Don Pwnious on Sat Sep 25, 2004 8:49 am, edited 2 times in total.
1/8th time- 14secs
1/8th speed - 110mph
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

I thought I mentioned that to you. Maybe I only mentioned it to Phel.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply