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

Discussion in 'C Programming' started by QQ, May 9, 2005.

  1. QQ

    QQ Guest

    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!
    QQ, May 9, 2005
    #1
    1. Advertising

  2. QQ <> 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 () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "The obvious mathematical breakthrough would be development of an easy way to
    factor large prime numbers."
    - Bill Gates
    Joona I Palaste, May 9, 2005
    #2
    1. Advertising

  3. QQ

    Richard Bos Guest

    Joona I Palaste <> wrote:

    > QQ <> 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
    Richard Bos, May 9, 2005
    #3
  4. QQ

    Mike Wahler Guest

    "QQ" <> wrote in message
    news:...
    >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
    Mike Wahler, May 9, 2005
    #4
  5. QQ

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, May 9, 2005
    #5
  6. In article <bqOfe.202$>,
    "Mike Wahler" <> wrote:

    > "QQ" <> wrote in message
    > news:...
    > >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
    Jonathan Adams, May 10, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fwee
    Replies:
    2
    Views:
    2,767
    Jeff Rodriguez
    Nov 17, 2003
  2. jwaixs

    Difference between return and exit

    jwaixs, Jul 24, 2005, in forum: C Programming
    Replies:
    16
    Views:
    868
  3. Vicky

    Difference between exit(0) & exit (1)

    Vicky, Aug 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    698
    Kenneth Brody
    Aug 8, 2006
  4. jacob navia
    Replies:
    3
    Views:
    535
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    667
    Tim Rentsch
    Mar 3, 2010
Loading...

Share This Page