Difference between using exit(n) and return n in main()?

F

fwee

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 one rather than the
other? Thanks!
 
J

Jack Klein

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 one rather than the
other? Thanks!

This is a FAQ:

11.16 Is exit(status) truly equivalent to returning the same status
from main?

See http://www.eskimo.com/~scs/C-faq/q11.16.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jeff Rodriguez

fwee said:
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 one rather than the
other? Thanks!
Exit exit the program with n status no matter how deeply you've recursed
in functions.

Example:

int blah (void)
{
return 1;
/* exit(1) would actually exit the program here, whereas return
does not */
}

int main (void)
{
blah();
return 0;
/* exit(0) and return have pretty much the same function in this
context */
}
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top