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

Q

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

Joona I Palaste

QQ said:
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 ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
R

Richard Bos

Joona I Palaste said:
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
 
M

Mike Wahler

QQ said:
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
 
C

CBFalconer

QQ said:
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.
 
J

Jonathan Adams

"Mike Wahler said:
'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()' 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
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top