what is the differrence b/w the usage or return and the exit in the C programming..

Discussion in 'C Programming' started by jayapal, Nov 2, 2007.

  1. jayapal

    jayapal Guest

    hi all,
    what is the differrence b/w the usage or return and the exit in the C
    programming..

    thanks,
    jay
     
    jayapal, Nov 2, 2007
    #1
    1. Advertising

  2. Re: what is the differrence b/w the usage or return and the exitin the C programming..

    jayapal wrote:
    > hi all,
    > what is the differrence b/w the usage or return and the exit in the C
    > programming..


    Within main(), not a lot.

    Within any other function, return ends the execution of the function
    immediately and returns to the caller, possibly also returning a value.
    exit() terminates the program.

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Nov 2, 2007
    #2
    1. Advertising

  3. jayapal

    quarkLore Guest

    Re: what is the differrence b/w the usage or return and the exit in the C programming..

    On Nov 2, 1:48 pm, jayapal <> wrote:
    > hi all,
    > what is the differrence b/w the usage or return and the exit in the C
    > programming..
    >
    > thanks,
    > jay


    exit is a function call return is a language keyword apart from many
    other differences.
     
    quarkLore, Nov 2, 2007
    #3
  4. jayapal

    santosh Guest

    jayapal wrote:

    > hi all,
    > what is the differrence b/w the usage or return and the exit in the C
    > programming..


    In C return is a keyword while exit() is a Standard library function.
    exit() accepts as argument a value of type int while return can
    supplied an expression yielding a value of any legal type. exit()
    causes termination of the program while return merely returns control
    to the function's caller. A special case is a return in main() which is
    roughly equivalent to an exit() call. exit() calls the functions
    registered with atexit() while for return this is only true when in
    main().

    Use exit() when you want to terminate normally from anywhere in the
    program. Use return for returning control to the function's caller.

    <http://www.c-faq.com/>
    <http://www.eskimo.com/~scs/cclass/>
    <http://clc-wiki.net/>
    <http://cprog.tomsweb.net/>
     
    santosh, Nov 2, 2007
    #4
  5. jayapal

    Flash Gordon Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Philip Potter wrote, On 02/11/07 09:09:
    > jayapal wrote:
    >> hi all,
    >> what is the differrence b/w the usage or return and the exit in the C
    >> programming..

    >
    > Within main(), not a lot.


    Why do people always forget about recursive calls to main? ;-)

    > Within any other function, return ends the execution of the function
    > immediately and returns to the caller, possibly also returning a value.
    > exit() terminates the program.


    exit() does not always terminate the program...

    #include <stdlib.h>

    void foo(void)
    {
    for (;;) continue;
    }

    int main(void)
    {
    atexit(foo);
    exit(0);
    }
    --
    Flash Gordon
    A revolting pedant.
     
    Flash Gordon, Nov 2, 2007
    #5
  6. In article <fgff88$sjl$>, santosh <> wrote:
    >jayapal wrote:
    >
    >> hi all,
    >> what is the differrence b/w the usage or return and the exit in the C
    >> programming..

    >
    >In C return is a keyword while exit() is a Standard library function.
    >exit() accepts as argument a value of type int while return can
    >supplied an expression yielding a value of any legal type. exit()
    >causes termination of the program while return merely returns control
    >to the function's caller. A special case is a return in main() which is
    >roughly equivalent to an exit() call. exit() calls the functions
    >registered with atexit() while for return this is only true when in
    >main().
    >
    >Use exit() when you want to terminate normally from anywhere in the
    >program. Use return for returning control to the function's caller.
    >
    ><http://www.c-faq.com/>
    ><http://www.eskimo.com/~scs/cclass/>
    ><http://clc-wiki.net/>
    ><http://cprog.tomsweb.net/>
    >


    Next question:
    what is the differrence b/w the usage of [the] printf and the gets in the C
    programming..?
     
    Kenny McCormack, Nov 2, 2007
    #6
  7. Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Kenny McCormack wrote:

    > Next question:
    > what is the differrence b/w the usage of [the] printf and the gets in the C
    > programming..?


    What is b/w? Black and white?

    What is the difference b/w a deposit and a withdrawal?

    Printf prints a formatted string to a standard output, gets reads a
    string from a standard input. Do not use gets, as it provides absolutely
    no control against buffer overflow. Use fgets instead.
     
    Peter Pichler, Nov 2, 2007
    #7
  8. Peter Pichler <> writes:
    > Kenny McCormack wrote:

    [...]
    > What is b/w? Black and white?
    >
    > What is the difference b/w a deposit and a withdrawal?
    >
    > Printf prints a formatted string to a standard output, gets reads a
    > string from a standard input. Do not use gets, as it provides
    > absolutely no control against buffer overflow. Use fgets instead.


    Please don't feed the troll.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 3, 2007
    #8
  9. Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Flash Gordon wrote:
    > Philip Potter wrote, On 02/11/07 09:09:
    >> jayapal wrote:
    >>> hi all,
    >>> what is the differrence b/w the usage or return and the exit in the C
    >>> programming..

    >>
    >> Within main(), not a lot.

    >
    > Why do people always forget about recursive calls to main? ;-)


    Because they're usually a bad idea?

    (Er, nice catch.)

    >> Within any other function, return ends the execution of the function
    >> immediately and returns to the caller, possibly also returning a
    >> value. exit() terminates the program.

    >
    > exit() does not always terminate the program...
    >
    > #include <stdlib.h>
    >
    > void foo(void)
    > {
    > for (;;) continue;
    > }
    >
    > int main(void)
    > {
    > atexit(foo);
    > exit(0);
    > }


    Perhaps, but I'm quite glad I missed this special case out of my
    original answer :)

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Nov 3, 2007
    #9
  10. jayapal

    Flash Gordon Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Philip Potter wrote, On 03/11/07 09:54:
    > Flash Gordon wrote:
    >> Philip Potter wrote, On 02/11/07 09:09:
    >>> jayapal wrote:
    >>>> hi all,
    >>>> what is the differrence b/w the usage or return and the exit in the C
    >>>> programming..
    >>>
    >>> Within main(), not a lot.

    >>
    >> Why do people always forget about recursive calls to main? ;-)

    >
    > Because they're usually a bad idea?


    I agree that they are a bad idea.

    > (Er, nice catch.)
    >
    >>> Within any other function, return ends the execution of the function
    >>> immediately and returns to the caller, possibly also returning a
    >>> value. exit() terminates the program.

    >>
    >> exit() does not always terminate the program...


    <snip example>

    > Perhaps, but I'm quite glad I missed this special case out of my
    > original answer :)


    Whenever anything is stated as an absolute truth someone, sometime, will
    find an exception that proves that it is not absolute.
    --
    Flash Gordon
    Making absolute statements that someone will now have to find exceptions to.
     
    Flash Gordon, Nov 3, 2007
    #10
  11. jayapal

    Tor Rustad Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    jayapal wrote:
    > hi all,
    > what is the differrence b/w the usage or return and the exit in the C
    > programming..


    After the initial call to the main function, those two are equivalent
    (assuming same value), except for one little detail, the exit(N) call
    doesn't end lifetime of objects with automatic storage duration declared
    in main.

    To illustrate, TC2 gives this example as UB:

    #include <stdio.h>
    #include <stdlib.h>

    void goodbye(void)
    {
    printf("Hello, World!\n");
    }

    int main(void)
    {
    char buf[20];
    setvbuf(stdout, buf, _IOFBF, sizeof(buf));
    atexit(goodbye);
    return 0;
    }

    while replacing "return 0" with exit(0) above, gives a strictly
    conforming program.


    --
    Tor < | tr i-za-h a-z>
     
    Tor Rustad, Nov 3, 2007
    #11
  12. Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Flash Gordon wrote:
    > Philip Potter wrote, On 03/11/07 09:54:
    >> Flash Gordon wrote:
    >>> Philip Potter wrote, On 02/11/07 09:09:
    >>>> jayapal wrote:
    >>>>> hi all,
    >>>>> what is the differrence b/w the usage or return and the exit in the C
    >>>>> programming..
    >>>>
    >>>> Within main(), not a lot.
    >>>
    >>> Why do people always forget about recursive calls to main? ;-)

    >>
    >> Because they're usually a bad idea?

    >
    > I agree that they are a bad idea.
    >
    >> (Er, nice catch.)
    >>
    >>>> Within any other function, return ends the execution of the function
    >>>> immediately and returns to the caller, possibly also returning a
    >>>> value. exit() terminates the program.
    >>>
    >>> exit() does not always terminate the program...

    >
    > <snip example>
    >
    >> Perhaps, but I'm quite glad I missed this special case out of my
    >> original answer :)

    >
    > Whenever anything is stated as an absolute truth someone, sometime, will
    > find an exception that proves that it is not absolute.


    But it is more educationally sound to start with simple truths and only
    mention the exceptions to those truths later on, than to find one cannot
    define exit() without also defining atexit().

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Nov 4, 2007
    #12
  13. jayapal

    Flash Gordon Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Philip Potter wrote, On 04/11/07 08:44:
    > Flash Gordon wrote:
    >> Philip Potter wrote, On 03/11/07 09:54:
    >>> Flash Gordon wrote:
    >>>> Philip Potter wrote, On 02/11/07 09:09:
    >>>>> jayapal wrote:


    <snip>

    >>> Perhaps, but I'm quite glad I missed this special case out of my
    >>> original answer :)

    >>
    >> Whenever anything is stated as an absolute truth someone, sometime,
    >> will find an exception that proves that it is not absolute.

    >
    > But it is more educationally sound to start with simple truths and only
    > mention the exceptions to those truths later on, than to find one cannot
    > define exit() without also defining atexit().


    Which is what we did.
    --
    Flash Gordon
     
    Flash Gordon, Nov 4, 2007
    #13
  14. Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Flash Gordon wrote:
    > Philip Potter wrote, On 04/11/07 08:44:
    >> But it is more educationally sound to start with simple truths and
    >> only mention the exceptions to those truths later on, than to find one
    >> cannot define exit() without also defining atexit().

    >
    > Which is what we did.


    Er.. I was working from the assumption that you were saying that I
    should have mentioned atexit() from the beginning, but looking back on
    the thread, it seems this is not the case.

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Nov 4, 2007
    #14
  15. jayapal

    Flash Gordon Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Philip Potter wrote, On 04/11/07 16:26:
    > Flash Gordon wrote:
    >> Philip Potter wrote, On 04/11/07 08:44:
    >>> But it is more educationally sound to start with simple truths and
    >>> only mention the exceptions to those truths later on, than to find
    >>> one cannot define exit() without also defining atexit().

    >>
    >> Which is what we did.

    >
    > Er.. I was working from the assumption that you were saying that I
    > should have mentioned atexit() from the beginning, but looking back on
    > the thread, it seems this is not the case.


    No, I was just being a pedantic awkward sod as was indicated in my sigs.
    --
    Flash Gordon
     
    Flash Gordon, Nov 4, 2007
    #15
  16. "Flash Gordon" <> a écrit dans le message de news:
    -gordon.me.uk...
    > Philip Potter wrote, On 03/11/07 09:54:
    >> Flash Gordon wrote:
    >>> Philip Potter wrote, On 02/11/07 09:09:
    >>>> jayapal wrote:
    >>>>> hi all,
    >>>>> what is the differrence b/w the usage or return and the exit in the C
    >>>>> programming..
    >>>>
    >>>> Within main(), not a lot.
    >>>
    >>> Why do people always forget about recursive calls to main? ;-)

    >>
    >> Because they're usually a bad idea?

    >
    > I agree that they are a bad idea.


    Not as bad as calling main from a function registered with atexit ;-)

    >> (Er, nice catch.)
    >>
    >>>> Within any other function, return ends the execution of the function
    >>>> immediately and returns to the caller, possibly also returning a value.
    >>>> exit() terminates the program.
    >>>
    >>> exit() does not always terminate the program...

    >
    > <snip example>
    >
    >> Perhaps, but I'm quite glad I missed this special case out of my original
    >> answer :)

    >
    > Whenever anything is stated as an absolute truth someone, sometime, will
    > find an exception that proves that it is not absolute.


    That's typical c.l.c experience, except for qualifiers: here this happens
    quickly and usually involves more than one responder.

    --
    Chqrlie.
     
    Charlie Gordon, Nov 7, 2007
    #16
  17. jayapal

    Flash Gordon Guest

    Re: what is the differrence b/w the usage or return and the exitin the C programming..

    Charlie Gordon wrote, On 07/11/07 14:22:
    > "Flash Gordon" <> a écrit dans le message de news:
    > -gordon.me.uk...
    >> Philip Potter wrote, On 03/11/07 09:54:
    >>> Flash Gordon wrote:
    >>>> Philip Potter wrote, On 02/11/07 09:09:
    >>>>> jayapal wrote:
    >>>>>> hi all,
    >>>>>> what is the differrence b/w the usage or return and the exit in the C
    >>>>>> programming..
    >>>>> Within main(), not a lot.
    >>>> Why do people always forget about recursive calls to main? ;-)
    >>> Because they're usually a bad idea?

    >> I agree that they are a bad idea.

    >
    > Not as bad as calling main from a function registered with atexit ;-)


    Now you are getting *really* nasty :)

    >>> (Er, nice catch.)
    >>>
    >>>>> Within any other function, return ends the execution of the function
    >>>>> immediately and returns to the caller, possibly also returning a value.
    >>>>> exit() terminates the program.
    >>>> exit() does not always terminate the program...

    >> <snip example>
    >>
    >>> Perhaps, but I'm quite glad I missed this special case out of my original
    >>> answer :)

    >> Whenever anything is stated as an absolute truth someone, sometime, will
    >> find an exception that proves that it is not absolute.

    >
    > That's typical c.l.c experience, except for qualifiers: here this happens
    > quickly and usually involves more than one responder.


    No, you've made the wrong response, you were meant to provide an
    exception my statement! ;-)

    Anyway, I think this thread has reached (or passed beyond) the point of
    being worth pursuing.
    --
    Flash Gordon
     
    Flash Gordon, Nov 7, 2007
    #17
    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. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,905
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,866
    sandeep1976
    Nov 8, 2006
  3. QQ
    Replies:
    5
    Views:
    521
    Jonathan Adams
    May 10, 2005
  4. jacob navia
    Replies:
    3
    Views:
    554
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    695
    Tim Rentsch
    Mar 3, 2010
Loading...

Share This Page