Page 1 of 1

Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 3:44 am
by xx6heartless6xx
As most of you know the main looks like this:

Code: Select all

int main(int argc, char* argv[] ) {

}
I didn't even know you can have parameters in the main function so when would you want to have parameters in your main function? Also can someone explain why these libraries' main functions have parameters when they are never used?

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 4:09 am
by ismetteren
It is the commandline arguments passed to the progam. The first int argument is the number of arguments and the second string array is the arguments themselves.

SDL and OpenGL does not have their own main function, you are using the regular plain old main function and calling SDL or OpenGL functions from it.jjj

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 8:58 am
by Falco Girgis
And it's not just SDL and OpenGL that have them. Make a blank C/++ project and add the same parameters (and try to print the first argument). You should technically always have them.

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 2:29 pm
by xx6heartless6xx
Okay so from now on I should always have them in every program? And how come my C++ book does not make me put it?

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 2:49 pm
by ismetteren
xx6heartless6xx wrote:Okay so from now on I should always have them in every program? And how come my C++ book does not make me put it?
They probably want to keep their basic examples as uncluttered as possible, so they can explain the very basics without having people thinking "What are those words and characters between the parenthesis for?".

Since Falco says that you should use them, i guess they are part of the specification, but most compilers let you omit them, since they aren't always used. I'm not quite sure about that though.

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 5:12 pm
by xx6heartless6xx
Thanks for clearing it up.

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 5:13 pm
by rolland
I'd keep them in every program. If nothing else, you at least get to feel like a professional bad-ass by including them ;)

Re: Why does SDL & OpenGL main have parameters

Posted: Wed Aug 31, 2011 10:37 pm
by Ginto8
It is not required to have main() parameters in any program other than SDL on windows, unless you are using command line arguments. Why SDL on windows? Because on windows, a graphical program doesn't have a main, it has a WinMain. So what does SDL do?

Code: Select all

#define main	SDL_main
extern C_LINKAGE int SDL_main(int argc, char *argv[]);
It pre-declares main so that it can run it in its WinMain. Because it's already predeclared, main() now can't work unless it has arguments. A hack? Definitely, but it works.