Difference between return and exit

J

jwaixs

Hello,

I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?

Also I was wondering if it whould be wise to combine the standard
status with return.
exp:

int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}

Greetz,

Noud Aldenhoven
 
S

S.Tobias

jwaixs said:
I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?

FAQ 11.16.
 
E

Eric Sosman

jwaixs said:
Hello,

I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?

They are almost equivalent. As far as I know there
are only two differences:

- The main() function can be called recursively, like
any other C function. If it is, only the return from
the first call terminates the program; they program
just keeps on running when a subsequent call returns.
Of course, exit() will terminate the program regardless.
Here's a silly program that prints its command-line
arguments in reverse order:

#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 0) {
main(argc - 1, argv + 1);
puts (*argv);
}
return 0;
}

Clearly, this program's behavior would be completely
different if exit() were used instead of return.

- When main() returns, all its local variables cease to
exist. If program wrap-up activities try to refer to
these variables (via pointers stored earlier), there
will probably be trouble. Functions registered with
atexit() are a potential source of trouble here; so are
setbuf() and setvbuf() if they use `auto' buffers and
their streams have not yet been closed. Calling exit(),
though, leaves the caller's variables intact.

Personally, I prefer to return from main rather than call
exit(), but it's a weak preference.
Also I was wondering if it whould be wise to combine the standard
status with return.
exp:

int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}

You should always return some kind of value from main() or
pass some kind of value to exit(). The three "portable" values
are 0, EXIT_SUCCESS, and EXIT_FAILURE; your system may possibly
recognize other values as well.
 
E

Emmanuel Delahaye

jwaixs wrote on 24/07/05 :
I was wondering, what's the difference between exit and return in the
main() function?

No visible difference. exit() is useless in main().
For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?

Also I was wondering if it whould be wise to combine the standard
status with return.

Yes, it would be.
int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}

Assuming you have included <stdio.h> and <stdlib.h>, there is nothing
wrong here.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
K

Kenny McCormack

jwaixs wrote on 24/07/05 :

No visible difference. exit() is useless in main().

False. It has been discussed in the past here why exit() is better.

And, of course, as the other poster noted, if main() is called recursively,
there's an obvious difference.
 
B

Barry Schwarz

Hello,

I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?

In any function, return transfers control back to the function that
called the current function.

In any function, exit transfers control to the system specific
environment that initiated the program of which the current function
is a part. It performs this transfer after calling any functions that
have been registered by atexit, flushing open files, and closing open
streams.

If main has not been entered recursively, if no functions have been
registered, if no files or streams are open, then the two are probably
equivalent.

If all of the above are not true, you should use return or exit
depending on whether you wish to return from this function to its
caller or exit this program completely.

It might make a difference if you keep in mind that one is a library
function and the other an operator in the language.
Also I was wondering if it whould be wise to combine the standard
status with return.
exp:

int main(){
printf("Hello World\n");
return EXIT_SUCCESS;

Wise is an insufficiently strong term. Try mandatory with only the
rarest of exceptions



<<Remove the del for email>>
 
K

Keith Thompson

False. It has been discussed in the past here why exit() is better.

It has? Other than in some exceedingly obscure circumstances, it's
exactly equivalent.
And, of course, as the other poster noted, if main() is called recursively,
there's an obvious difference.

If main() is called recursively, there's an obvious problem. Though
it's allowed by the language, it's not something I'd ever do outside
the IOCCC or a compiler test.
 
K

Keith Thompson

Barry Schwarz said:
I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?
[...]
It might make a difference if you keep in mind that one is a library
function and the other an operator in the language.

A quibble: return is not an operator.
 
K

Kenny McCormack

It has? Other than in some exceedingly obscure circumstances, it's
exactly equivalent.

Think atexit(). I'm not going to spell it out for you.
If main() is called recursively, there's an obvious problem. Though
it's allowed by the language, it's not something I'd ever do outside
the IOCCC or a compiler test.

That doesn't matter one tiny bit. According to the verbiage I've been
reading here lately, doing so is legal (standards compliant) in C (but not
in C++).
 
K

Keith Thompson

Think atexit(). I'm not going to spell it out for you.

You don't need to. atexit() is relevant only if a function registered
via atexit() makes use of variables declared local to main(). A
return statement leaves the main() function and then invokes any
registered functions; a call to exit() invokes any registered
functions and then leaves the main() function.

In my opinion, an atexit()-registered function that depends on
variables local to main() constitutes "exceedingly obscure
circumstances".

Even the standard says that "a return from the initial call to the
main function is equivalent to calling the exit function with the
value returned by the main function as its argument", though it
mentions the lifetime of objects declared within main() in a footnote.

[...]
That doesn't matter one tiny bit. According to the verbiage I've been
reading here lately, doing so is legal (standards compliant) in C (but not
in C++).

I don't dispute that there's a difference between return and exit().
I'm just saying that the difference rarely matters. It matters only
in certain well-defined circumstances that are easy to avoid.
 
S

Steve Summit

Keith said:
If main() is called recursively, there's an obvious problem.

Is there? Necessarily? I admit that recursive calls to main are
quite rare, and they're usually obfuscated or at best misguided,
but I've seen and written code, a handful of times, where such a
call was useful and not inappropriate...
 
K

Kenny McCormack

Is there? Necessarily? I admit that recursive calls to main are
quite rare, and they're usually obfuscated or at best misguided,
but I've seen and written code, a handful of times, where such a
call was useful and not inappropriate...

Keith's been falling off the dogma wagon lately.
 
K

Keith Thompson

Is there? Necessarily? I admit that recursive calls to main are
quite rare, and they're usually obfuscated or at best misguided,
but I've seen and written code, a handful of times, where such a
call was useful and not inappropriate...

Ok, perhaps not necessarily. I find it difficult to imagine a case
where a recursive call to main would actually make sense, but my
imagination is admittedly finite.

I'd probably prefer to write a separate recursive function and call it
from main(); for one thing, that neatly avoids any obscure issues
regarding return vs. exit().

I'll also mention that some of the few cases of recursive main() that
I've seen were unnecessary (e.g., could have been better implemented
as a simple loop).
 
C

CBFalconer

Kenny said:
.... snip ...

That doesn't matter one tiny bit. According to the verbiage I've
been reading here lately, doing so is legal (standards compliant)
in C (but not in C++).

Trying to find a legitimate use for recursive mainery, and I came
up with:

#include <stdio.h>

static int copyfile(char * fname) {
/* gyrations to open, copy to stdout, close */
return status;
}

int main(int argc, char **argv)
{
if (argc > 1) {
copyfile(argv[1]);
main(argc - 1, argv[1]);
}
return 0;
}

which I believe will copy a list of files given on the command
line.
 
K

Keith Thompson

CBFalconer said:
Trying to find a legitimate use for recursive mainery, and I came
up with:

#include <stdio.h>

static int copyfile(char * fname) {
/* gyrations to open, copy to stdout, close */
return status;
}

int main(int argc, char **argv)
{
if (argc > 1) {
copyfile(argv[1]);
main(argc - 1, argv[1]);
}
return 0;
}

which I believe will copy a list of files given on the command
line.

I think the recursive call should be

main(argc - 1, argv + 1);

(And I'd still prefer a loop, but that's mostly a matter of style.)
 
B

Barry Schwarz

Barry Schwarz said:
I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation?
[...]
It might make a difference if you keep in mind that one is a library
function and the other an operator in the language.

A quibble: return is not an operator.

Yes, I should have said keyword.


<<Remove the del for email>>
 
M

Mark

Barry Schwarz said:
Barry Schwarz said:
I was wondering, what's the difference between exit and return in the
main() function? For me they both look the same, or aren't they? And if
they aren't, which should I use in which situation? [...]
It might make a difference if you keep in mind that one is a library
function and the other an operator in the language.

A quibble: return is not an operator.

Yes, I should have said keyword.

Or better yet: statement
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top