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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • QQ

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

    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

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

    QQ <junciu@yahoo.c om> scribbled the following:[color=blue]
    > I know there are many functions that I can exit the program such as
    > return 0,
    > exit(0), exit(1),_EXIT(0 ) ....[/color]
    [color=blue]
    > What are the difference between them?[/color]
    [color=blue]
    > Thanks a lot![/color]

    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 (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "The obvious mathematical breakthrough would be development of an easy way to
    factor large prime numbers."
    - Bill Gates

    Comment

    • Richard Bos

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

      Joona I Palaste <palaste@cc.hel sinki.fi> wrote:
      [color=blue]
      > QQ <junciu@yahoo.c om> scribbled the following:[color=green]
      > > I know there are many functions that I can exit the program such as
      > > return 0,
      > > exit(0), exit(1),_EXIT(0 ) ....[/color]
      >[color=green]
      > > What are the difference between them?[/color]
      >[color=green]
      > > Thanks a lot![/color]
      >
      > 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.[/color]

      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

      Comment

      • Mike Wahler

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

        "QQ" <junciu@yahoo.c om> wrote in message
        news:1115648799 .576570.16390@z 14g2000cwz.goog legroups.com...[color=blue]
        >I know there are many functions that I can exit the program such as
        > return 0,[/color]

        '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()'.
        [color=blue]
        > exit(0), exit(1),_EXIT(0 ) ....[/color]

        'exit()' is a standard C function, _EXIT() is not.
        [color=blue]
        >
        > What are the difference between them?[/color]

        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


        Comment

        • CBFalconer

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

          QQ wrote:[color=blue]
          >
          > 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?[/color]

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

          --
          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net> USE worldnet address!


          Comment

          • Jonathan Adams

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

            In article <bqOfe.202$2u5. 41@newsread1.ne ws.pas.earthlin k.net>,
            "Mike Wahler" <mkwahler@mkwah ler.net> wrote:
            [color=blue]
            > "QQ" <junciu@yahoo.c om> wrote in message
            > news:1115648799 .576570.16390@z 14g2000cwz.goog legroups.com...[color=green]
            > >I know there are many functions that I can exit the program such as
            > > return 0,[/color]
            >
            > '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()'.
            >[color=green]
            > > exit(0), exit(1),_EXIT(0 ) ....[/color]
            >
            > 'exit()' is a standard C function, _EXIT() is not.[/color]

            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

            Comment

            Working...