473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Difference between return and exit

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

Nov 15 '05 #1
17 8001
jwaixs <jw****@gmail.com> wrote:
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.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #2
jwaixs wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
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."
Nov 15 '05 #4
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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().


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.

Nov 15 '05 #5
On 24 Jul 2005 06:48:49 -0700, "jwaixs" <jw****@gmail.com> wrote:
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>>
Nov 15 '05 #6
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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().


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7
Barry Schwarz <sc******@deloz.net> writes:
On 24 Jul 2005 06:48:49 -0700, "jwaixs" <jw****@gmail.com> wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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().


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.


Think atexit(). I'm not going to spell it out for you.
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.


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++).

Nov 15 '05 #9
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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().

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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
Keith Thompson evidently wrote:
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...
Nov 15 '05 #11
In article <dc**********@eskinews.eskimo.com>,
Steve Summit <sc*@eskimo.com> wrote:
Keith Thompson evidently wrote:
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...


Keith's been falling off the dogma wagon lately.

Nov 15 '05 #12
sc*@eskimo.com (Steve Summit) writes:
Keith Thompson evidently wrote:
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...


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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13
Kenny McCormack wrote:
Keith Thompson <ks***@mib.org> wrote:

.... snip ...

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++).


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #14
CBFalconer <cb********@yahoo.com> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #15
On Sun, 24 Jul 2005 19:27:14 GMT, Keith Thompson <ks***@mib.org>
wrote:
Barry Schwarz <sc******@deloz.net> writes:
On 24 Jul 2005 06:48:49 -0700, "jwaixs" <jw****@gmail.com> wrote:
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>>
Nov 15 '05 #16

"Barry Schwarz" <sc******@deloz.net> wrote in message
news:oe********************************@4ax.com...
On Sun, 24 Jul 2005 19:27:14 GMT, Keith Thompson <ks***@mib.org>
wrote:
Barry Schwarz <sc******@deloz.net> writes:
On 24 Jul 2005 06:48:49 -0700, "jwaixs" <jw****@gmail.com> wrote:
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
Nov 15 '05 #17
Keith Thompson wrote:
CBFalconer <cb********@yahoo.com> writes:
[...]
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.)


Calling main() sounds like a bad idea!
Nov 15 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard | last post by:
Hi, Can anyone tell me what the difference is between for line in file.readlines( ): and for line in file:
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
17
by: ambar.shome | last post by:
Can anyone tell me : 1. void main() { char temp = "Hello"; temp='K';
2
by: fwee | last post by:
What is the difference between using exit(n) and return n in main(), if any? I know that exit(n) is a function that exits and return n exits via a return value, but is there any reason to specify...
5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
6
by: Vicky | last post by:
Please tell me at vdkhakhkhar@gmail.com
5
by: Antonio Parolini | last post by:
What is the difference between: main(......) { exit(0); } and main(......) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.