Valhalla Legends Archive

Programming => General Programming => Topic started by: MailMan on March 27, 2003, 07:25 PM

Title: main function [C++]
Post by: MailMan on March 27, 2003, 07:25 PM
I've seen main functions with arguments before. ie:


int main(int something, int something_else)


What's passed to the main function? Is that for like.. variables passed via command line or something?
Title: Re:main function [C++]
Post by: iago on March 27, 2003, 07:43 PM
Standard main is:
int main(int argc, char *argv[])

argc is the number of commandline arguments, and argv is an array of the arguments,
Title: Re:main function [C++]
Post by: MailMan on March 27, 2003, 08:56 PM
Cool, thanks!
Title: Re:main function [C++]
Post by: Skywing on March 28, 2003, 11:41 PM
Quote from: iago on March 27, 2003, 07:43 PM
Standard main is:
int main(int argc, char *argv[])

argc is the number of commandline arguments, and argv is an array of the arguments,
  • being the name of the program.
Don't forget:
int main(int argc, char *argv[], char *envp[])
Title: Re:main function [C++]
Post by: Zakath on March 29, 2003, 01:34 AM
I've not seen that one...what's the third param for? What would a second array of strings do there?
Title: Re:main function [C++]
Post by: iago on March 29, 2003, 03:22 AM
I would guess they are environmental variables, seeing as though it's called envp :)
Title: Re:main function [C++]
Post by: Yoni on March 29, 2003, 04:33 AM
Yes.

All three forms (0, 2, 3 parameters) are acceptable.
Title: Re:main function [C++]
Post by: Skywing on March 29, 2003, 11:02 AM
Quote from: Yoni on March 29, 2003, 04:33 AM
Yes.

All three forms (0, 2, 3 parameters) are acceptable.
void main() is not acceptable, however, despite what Visual C++ might try to tell you.
Title: Beware
Post by: Kp on March 29, 2003, 06:23 PM
envp is Windows specific, iirc.  Use getenv() instead.
Title: Re:Beware
Post by: Skywing on March 29, 2003, 07:10 PM
Quote from: Kp on March 29, 2003, 06:23 PM
envp is Windows specific, iirc.  Use getenv() instead.
Ah, you're right.  According to documentation, I've found, it's something most Windows and UNIX compilers implement, but not a standard feature.
Title: Re:main function [C++]
Post by: Etheran on April 08, 2003, 04:01 AM
int main(int argc, wchar_t *argv[]);
is this not supported?
Title: Re:main function [C++]
Post by: Yoni on April 08, 2003, 09:30 AM
Quote from: Etheran on April 08, 2003, 04:01 AM
int main(int argc, wchar_t *argv[]);
is this not supported?
If you want the argv argument to be a wide string, you'll have to rename main to wmain.

MSVC++ also allows the following:
int _tmain(int argc, TCHAR* argv[]);

Similarly with WinMain:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nShow);
// or
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nShow);
// or
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nShow);
Title: Re:main function [C++]
Post by: MrRaza on April 08, 2003, 07:00 PM
I've also seen void main(void) {...} (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....
Title: Re:main function [C++]
Post by: Eibro on April 08, 2003, 07:11 PM
Quote from: MrRaza on April 08, 2003, 07:00 PM
I've also seen void main(void) {...} (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....
No, that's not standard C (or C++). http://www.eskimo.com/~scs/readings/voidmain.960823.html
void main is simply wrong.
Title: Re:main function [C++]
Post by: MrRaza on April 08, 2003, 07:32 PM
Well, i guess my C++ book is wrong, but hey atleast it has some good AI information in it..