Computer Science Canada Quick Question on main() |
Author: | Clayton [ Thu Jan 22, 2009 12:31 pm ] | ||||||
Post subject: | Quick Question on main() | ||||||
So, I've been toying around with C for some time, and now I have a class utilizing it for a Systems Programming class here at school. However, this is the first time with C for some of my friends, and for others it's not their first time, but they haven't done much with it. However, one dispute that has already come up, is conventions when defining main(). I believe the way I do it is the more generally "accepted" way, whereas they don't. Both are right in terms that they compile, but I'd just like to confirm which is better style for C. For reference, the ways disputed between us is:
And:
As well as:
What do you guys think? Along with why would be awesome |
Author: | DemonWasp [ Thu Jan 22, 2009 12:51 pm ] |
Post subject: | RE:Quick Question on main() |
Technically, the return from the main() function is used to tell the calling process whether the program executed properly (returns 0) or did not (returns some error code, which can then be looked up against the documentation for the program). Unless I'm much mistaken, this was commonplace long before proper exception-handling and non-shell programs. The correct solution then depends largely on what you're coding for. If it's on a command-line, it should return an integer expressing success / failure and failure type, so you'd want the first version. If it's not for command-line use, you can use any form (though I would still tend to use the first, since it can then ALSO be called from the command-line). |
Author: | Clayton [ Thu Jan 22, 2009 3:07 pm ] |
Post subject: | RE:Quick Question on main() |
In terms of style, which is better though? |
Author: | jernst [ Thu Jan 22, 2009 3:55 pm ] | ||
Post subject: | Re: Quick Question on main() | ||
+1 demon, I agree with what you said, I also prefer returning int from main. Clayton don't forget there are also cases with parameters as well :
|
Author: | md [ Thu Jan 22, 2009 4:24 pm ] | ||
Post subject: | RE:Quick Question on main() | ||
The correct header for main is
void main() is right out by virtue of having the wrong return type (and gcc *will* complain). And you always need to return something, you never know how your program will be called. I wish to god people would stop using void main()... at least my prof was good enough to fail people who did (on the code part). |
Author: | wtd [ Thu Jan 22, 2009 5:12 pm ] |
Post subject: | RE:Quick Question on main() |
In this case md is right, and everyone else is wrong. Except me, for agreeing with him. |
Author: | md [ Thu Jan 22, 2009 6:37 pm ] | ||||||
Post subject: | RE:Quick Question on main() | ||||||
I should note that
|
Author: | OneOffDriveByPoster [ Thu Jan 22, 2009 9:46 pm ] | ||||||||||
Post subject: | Re: RE:Quick Question on main() | ||||||||||
md @ Thu Jan 22, 2009 6:37 pm wrote: I should note that Nope. Syntax error on
|
Author: | btiffin [ Thu Jan 22, 2009 9:54 pm ] | ||
Post subject: | Re: Quick Question on main() | ||
The C99 standard lists
Though I'll admit to only having read the publically available Drafts and not the final sanctioned ISO/IEC JTC1/SC22/WG14 9899 text. See http://www.open-std.org/jtc1/sc22/wg14/www/projects#9899 Cheers |
Author: | [Gandalf] [ Thu Jan 22, 2009 10:28 pm ] | ||||||||
Post subject: | RE:Quick Question on main() | ||||||||
As you can see there is clearly no one correct answer, since there are so many variations of main() and so many compilers and dated standards. Personally, I use:
and:
and perhaps rarely:
Really the only one I'd kill someone over is:
Or some such, but maybe that's just my relaxed nature. |
Author: | A.J [ Thu Jan 22, 2009 10:40 pm ] | ||
Post subject: | Re: Quick Question on main() | ||
I would prefer :
As far as convention goes, I really think it is up to the coder and the context he is using it in. As for me though, I am used to coding like this for contests and other stuff, but I have no right in saying that this is the 'right' way of doing it. It is a matter of familiarization. |
Author: | Clayton [ Fri Jan 23, 2009 12:04 pm ] | ||
Post subject: | Re: Quick Question on main() | ||
btiffin @ Thu Jan 22, 2009 9:54 pm wrote: The C99 standard lists
Indeed. |
Author: | md [ Sat Jan 24, 2009 1:47 pm ] | ||
Post subject: | RE:Quick Question on main() | ||
In any case -
|
Author: | wtd [ Sat Jan 24, 2009 2:35 pm ] | ||
Post subject: | Re: RE:Quick Question on main() | ||
md @ Sun Jan 25, 2009 2:47 am wrote: In any case -
md is very much correct. There is no harm in having the argument parameters, so you might as well do it for consistency's sake. Oh, and Dan: we need the ability to in-line fixed width text. |