469,632 Members | 1,656 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

main exit/return

Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?

Thanks in advance ! ! !
Jun 27 '08 #1
11 2538
Rahul wrote:
Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
That's a bit ancient.
{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?
For the head:

int main(void) { ... }
int main( int argc, char **argv ) { ... }

(and trivial equivalent syntaxes)

For the return:

return 0;
return EXIT_SUCCESS;

return EXIT_FAILURE;

(there's an escape-hatch for no return at all; avoid.)

(`return` doesn't need parentheses; the standard doesn't
exhibit a preference, but I see no reason to use `()` when
they're completely irrelevant, and at least one reason -- to
emphasise that `return` is not a function call -- to leave
them out.)

The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

--
"The original and modest plans had to be continually extended."/Sector General/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #2
On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
Jun 27 '08 #3
On Jun 3, 11:55 am, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);

}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);

}

I'm not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?
Question 11.4 of the C-FAQ.
<http://c-faq.com/>
Jun 27 '08 #4
christian.bau wrote:
On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

--
"The letter was not unproductive." /Mansfield Park/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #5
Chris Dollin <ch**********@hp.comwrites:
christian.bau wrote:
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
I thought I had come up with a plausible example, but I don't think
I actually have.

Suppose main() saves copies of argc and argv in "global" variables
saved_argc and saved_argv, so they can be accessed by other functions.
An atexit-registered cleanup routine uses these to access the
command-line arguments for whatever reason.

But then I looked at C99 5.1.2.2.1p2:

If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup.

[...]

The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program, and retain their
last-stored values between program startup and program
termination.

The strings pointed to by the argv array retain their values until
*program termination*, not just until termination of the main()
function.

If a program saved *pointers to* argc and argv, then there could be a
problem, but I can't think of any good reason to do that rather than
saving copies of them.

The standard is vague about the argv array itself (i.e., the array of
char* to whose first element the argv parameter points). It *should*
say that this array also survives until program termination, and I
think it's reasonably safe to assume that it does (other than on the
DS9K).

(Question: Does the DS9K implementation exploit poor wording, or does
it just exploit freedoms deliberately offered by the standard to
absurd extremes?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
Chris Dollin wrote:
christian.bau wrote:
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call
exit () from main, local variables of the main function will still
exist when these functions execute; if you use a return statement
then local variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Jun 27 '08 #7
santosh <sa*********@gmail.comwrites:
Chris Dollin wrote:
>christian.bau wrote:
>>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:

The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call
exit () from main, local variables of the main function will still
exist when these functions execute; if you use a return statement
then local variables of main are gone.

That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?
Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
Keith Thompson said:
santosh <sa*********@gmail.comwrites:
<snip>
>Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.
Recursive main is one of those techniques that every C programmer should
use occasionally. It's like int delete, or struct T *new, or x =
a//*divide a by b */b, and so on. I'm not saying you should do it too
much, mind you - just... um... now and again.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
>>The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
>christian.bau wrote:
>There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.
In article <g2*********@news-pa1.hpl.hp.com>
Chris Dollin <ch**********@hp.comwrote:
>That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".
The classic example problem is:

#include <stdio.h>

int main(void) {
char buf[BUFSIZ];

setbuf(stdout, buf);
... do some work ...
return 0;
}

Here, the setbuf() call sets "stdout" (a "global variable" of sorts,
depending on exactly how one defines "global variable") so that it
refers to the local variable "buf" in main(). The local variable
ceases to exist upon return from main(), so when the C system's
startup code does the equivalent of:

exit(main(argc, argv));

and when the exit() call tries to fflush(stdout) -- either because
there is pending output, or because of an active atexit() at that
point that calls printf() and/or fflush() -- things go wrong.

(The solution involves using either a static-duration buffer, or
malloc(), when using setbuf() or setvbuf(). Either way, one gets
a buffer whose storage duration persists until all atexit() handlers
have run and stdout itself is no longer needed.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #10
Chris Torek wrote:
>>On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:
The same values apply to the argument of `exit`, but why use
`exit` when you're in `main`?
>>christian.bau wrote:
>>There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.

In article <g2*********@news-pa1.hpl.hp.com>
Chris Dollin <ch**********@hp.comwrote:
>>That's a nice [1] gotcha; I hadn't thought of that. I'm trying to
think of a natural example where it would matter ...

[1] As in "oh noes, my brain just melted".

The classic example problem is:

#include <stdio.h>

int main(void) {
char buf[BUFSIZ];

setbuf(stdout, buf);
... do some work ...
return 0;
}
Thanks, Chris; I don't use setbuf often enough to have thought of that ...

--
"I am your new best friend." /Electra City/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #11
Richard Heathfield wrote:
Keith Thompson said:
>santosh <sa*********@gmail.comwrites:

<snip>
>>Maybe one should use return when main has been called recursively and
exit() for a normal termination of main?

Maybe one shouldn't call main recursively. You can always write a
recursive function and call it from main.

Recursive main is one of those techniques that every C programmer should
use occasionally. It's like int delete, or struct T *new, or x =
a//*divide a by b */b, and so on. I'm not saying you should do it too
much, mind you - just... um... now and again.
And you can stop whenever you want?

--
"The original and modest plans had to be continually extended."/Sector General/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

192 posts views Thread by Kwan Ting | last post: by
45 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by Seong-Kook Shin | last post: by
20 posts views Thread by small TUX | last post: by
8 posts views Thread by Michal Nazarewicz | last post: by
9 posts views Thread by DanielJohnson | last post: by
27 posts views Thread by junky_fellow | last post: by
29 posts views Thread by pereges | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.