473,395 Members | 1,730 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,395 software developers and data experts.

What's the difference of return 0; exit(0);exit(1)

QQ
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!

Nov 14 '05 #1
5 28004
QQ <ju****@yahoo.com> scribbled the following:
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!


The difference between return and exit() is that return only ends the
current function, while exit() ends the whole program. In main(),
return and exit() are identical.
As for the numbers, 0 means successful completion. 1 is non-standard,
and can mean whatever the implementation pleases. For standard code,
use EXIT_SUCCESS and EXIT_FAILURE.
_EXIT() is a non-standard implementation-specific function.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 14 '05 #2
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
QQ <ju****@yahoo.com> scribbled the following:
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!


The difference between return and exit() is that return only ends the
current function, while exit() ends the whole program. In main(),
return and exit() are identical.


Only in a sane program. If you call main() recursively (or, even worse,
indirectly recursively), returning from main() will only return from the
current invocation, and exit() is (more or less) equivalent to a return
from the outermost (i.e., first) call of main().
And then there is the jolly trick of using atexit() functions which
require that local variables in main() still exist, or similar
perversions using setbuf().
Clearly, neither of these behaviours are in the least recommendable in a
well-behaved, sanely written C program. But they can occur, and when
they do, there is a difference between exit() and return from main().

Richard
Nov 14 '05 #3
"QQ" <ju****@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I know there are many functions that I can exit the program such as
return 0,
'return' is not a function, it's a keyword. The only
way to use 'return' to terminate your program is to
write it inside the function 'main()'.
exit(0), exit(1),_EXIT(0) ....
'exit()' is a standard C function, _EXIT() is not.

What are the difference between them?


You can use the function 'exit()' to terminate your
program at any point where an executable statement
is valid (i. e. inside a function body, which need
not be that of 'main()'.) The only portable arguments
for 'exit()' are zero (0), and 'EXIT_SUCCESS' or
'EXIT_FAILURE' (those are macros declared by <stdlib.h>).

Which C book(s) are you reading which don't explain this?

-Mike
Nov 14 '05 #4
QQ wrote:

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?


"return 0" will exit some functions. exit(0) will exit a program.
The rest will cause undefined (or possibly implementation defined)
behaviour.

--
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 14 '05 #5
In article <bq**************@newsread1.news.pas.earthlink.net >,
"Mike Wahler" <mk******@mkwahler.net> wrote:
"QQ" <ju****@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I know there are many functions that I can exit the program such as
return 0,


'return' is not a function, it's a keyword. The only
way to use 'return' to terminate your program is to
write it inside the function 'main()'.
exit(0), exit(1),_EXIT(0) ....


'exit()' is a standard C function, _EXIT() is not.


Perhaps he meant _Exit(), which is defined in C99. _Exit() will not
call functions registered with atexit() nor any registered signal
handlers. Most of the rest of its behavior is implementation-defined.

Cheers,
- jonathan
Nov 14 '05 #6

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

Similar topics

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; }
4
by: John Black | last post by:
Hi, I wonder what is the difference between ending the main() with and without exit(0)? My code is like this flow, class ClassA{ ClassB* cb; int toExit();
7
by: Darklight | last post by:
if the exit() function is contained in stdlid.h why if i remove the above header file the exit function still works and i can still compile program without any error messages using cc file.c if i...
17
by: jwaixs | last post by:
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...
3
by: John Williams | last post by:
VB .Net 2003, .Net Framework 1.1, Windows XP Pro SP1 I've written a console application which I run as a scheduled task. How do I return an exit status value (or string) from the application so...
4
by: Wardeaux | last post by:
Hope this is easy... I have a simple winform app that does some work and then exits... I need to return a value to the calling app/process to indicate completion status... what's the...
3
by: asearle | last post by:
Hallo everyone, I have a perl script which can return error codes. I have done this with, for example, 'exit 11;', which would (I imagine) send the exit code 11 (instead of 0) back to the calling...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
39
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.