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

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

QQ
Guest
Posts: n/a

 05-09-2005
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!

Joona I Palaste
Guest
Posts: n/a

 05-09-2005
QQ <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates

Richard Bos
Guest
Posts: n/a

 05-09-2005
Joona I Palaste <(E-Mail Removed)> wrote:

> QQ <(E-Mail Removed)> 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

Mike Wahler
Guest
Posts: n/a

 05-09-2005
"QQ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

CBFalconer
Guest
Posts: n/a

 05-09-2005
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Guest
Posts: n/a

 05-10-2005
In article <bqOfe.202\$(E-Mail Removed) >,
"Mike Wahler" <(E-Mail Removed)> wrote:

> "QQ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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