Quick Question on main()
Author |
Message |
Clayton
|
Posted: 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:
c: | #include <stdio.h>
int main ()
{
printf("Hello, world!\n");
return 0;
} |
And:
c: | #include <stdio.h>
void main ()
{
printf("Hello, world!\n");
return;
} |
As well as:
c: | #include <stdio.h>
main ()
{
printf("Hello, world!\n");
return;
} |
What do you guys think? Along with why would be awesome |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: 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). |
|
|
|
|
|
Clayton
|
Posted: Thu Jan 22, 2009 3:07 pm Post subject: RE:Quick Question on main() |
|
|
In terms of style, which is better though? |
|
|
|
|
|
jernst
|
Posted: 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 :
code: | int main(int argc, char * argv[]) ... |
|
|
|
|
|
|
md
|
Posted: Thu Jan 22, 2009 4:24 pm Post subject: RE:Quick Question on main() |
|
|
The correct header for main is C: | int main(int argc, char** argv) |
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). |
|
|
|
|
|
wtd
|
Posted: 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. |
|
|
|
|
|
md
|
Posted: Thu Jan 22, 2009 6:37 pm Post subject: RE:Quick Question on main() |
|
|
I should note that is also valid, but and are not. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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 is also valid, but and are not. Nope. Syntax error on . Look at the grammar in Sub-clause 6.7.5 of ISO/IEC 9899:1999.
code: | parameter-type-list:
parameter-list
parameter-list , ... | . The other forms you mention are perfectly acceptable (see ISO/IEC 9899:1999 Sub-clause 5.1.2.2.1 paragraph 1). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
btiffin
|
Posted: Thu Jan 22, 2009 9:54 pm Post subject: Re: Quick Question on main() |
|
|
The C99 standard lists code: | int main(void)
int main(int argc, char *argv[]) | as valid forms that shall be supported.
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 |
|
|
|
|
|
[Gandalf]
|
Posted: 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:
code: | int main(int argc, char **argv) |
and perhaps rarely:
code: | int main(int argc, char *argv[]) |
Really the only one I'd kill someone over is:
Or some such, but maybe that's just my relaxed nature. |
|
|
|
|
|
A.J
|
Posted: Thu Jan 22, 2009 10:40 pm Post subject: Re: Quick Question on main() |
|
|
I would prefer :
c: |
int main()
{
// your code goes here
system("PAUSE");
return 0;
}
|
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. |
|
|
|
|
|
Clayton
|
Posted: 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 code: | int main(void)
int main(int argc, char *argv[]) | as valid forms that shall be supported.
Indeed. |
|
|
|
|
|
md
|
Posted: Sat Jan 24, 2009 1:47 pm Post subject: RE:Quick Question on main() |
|
|
In any case - C: | int main(int argc, char** argv) | , is my preferred header. |
|
|
|
|
|
wtd
|
Posted: 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 - C: | int main(int argc, char** argv) | , is my preferred header.
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. |
|
|
|
|
|
|
|