Why does SDL & OpenGL main have parameters

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
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Why does SDL & OpenGL main have parameters

Post 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?
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Why does SDL & OpenGL main have parameters

Post 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
Image ImageImage Image
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:

Re: Why does SDL & OpenGL main have parameters

Post 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.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: Why does SDL & OpenGL main have parameters

Post 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?
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Why does SDL & OpenGL main have parameters

Post 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.
Image ImageImage Image
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: Why does SDL & OpenGL main have parameters

Post by xx6heartless6xx »

Thanks for clearing it up.
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

Re: Why does SDL & OpenGL main have parameters

Post 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 ;)
I'll write a signature once I get some creativity and inspiration...
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Why does SDL & OpenGL main have parameters

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply