Thread: exit() vs return()

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    exit() vs return()

    Would you clarify when and why you would use “return 0;” inside a main() as opposed to exit(0) ?

    Exit(0) would have cleanup code that return(0) does not? True?

    Code:
    /* exittest1.c */
    
    #include<stdio.h>
    
    int main() {
    
    int a;
     a = 69;
    exit(a);
    }
    and

    Code:
    /* exittest1.c */
    
    #include<stdio.h>
    
    int main() {
    
    int a;
     a = 69;
    return(a);
    }
    How do you obtain the value returned?

    “The stuppider I get the smarter I feel”

    tia meow


    ps do not recall seeing return 0; inside main() before. was looking through the faq and noticed some have it in the examples and some not.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You'd use return whenever there's no compelling reason to use exit(). In C++ in particular, note that exit() does not return and objects on the stack of main() won't get destroyed. Compare:
    Code:
    #include <fstream>
    #include <cstdlib>
    
    int main()
    {
      std::ofstream os("out.txt");
      os << "Hello, there!\n"; // Note the lack of flushing.
      std::exit(0);
    }
    vs.
    Code:
    #include <fstream>
    #include <cstdlib>
    
    int main()
    {
      std::ofstream os("out.txt");
      os << "Hello, there!\n"; // Note the lack of flushing.
      // Implicit return 0.
    }
    I'm sure it would be fairly easy to find a platform where after running the first example, out.txt is empty. It would be hard to find one where the same holds for example two, and if you find one, then it's got a seriously broken iostreams implementation.


    As for obtaining the returned value, that's highly system specific. DOS or Win32 cmd.exe would put it in the ERRORLEVEL system variable. Bash lets you fetch it with $?, or you can just use && or || or similar to act on it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    ok thank you for the answer. still a little confused.

    after first prog run out.txt = 0 bytes


    Directory of C:\borland\bcc55\bin

    10/19/2006 07:15 PM 0 out.txt
    1 File(s) 0 bytes


    after second prog run out.txt = 15 bytes


    Directory of C:\borland\bcc55\bin

    10/19/2006 07:19 PM 15 out.txt
    1 File(s) 15 bytes

    i get this with the out.txt so there is always a return 0 even if you do not code one there? and the choise to use exit() or return() depends if you want the var destroyed durning exit of prog? that is also true for c? yes?

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Actually, return isn't a function.
    It is usually just written like "return 0;".

    Return is built-in, exit() is part of a library.
    I would use return
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm not much of a C programmer, but a quick Google shows that exit() should be OK in C programs. Especially, if your program meets an error condition (such as a division by zero ahead) in some nested function (function called by function called by ... called by main), and you think there's no way your program could continue, you can use exit(errorcode) to quit the program immediately (rather than find the long and twisted way back to the main function to use return there).

    In C++, exit doesn't seem to be that good, because if it is used class destructors are not called, and sometimes the destructor may do important stuff (as the example above shows). C++ of course has its own error handling mechanisms, so that finding the way back to main() could be done by the compiler. But since C doesn't have classes, exit won't have this problem with it.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Ok, I'll correct myself.
    I would use return if I'm in the main() function.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Of course you can always use atexit() if you want to do some cleaning up in C


  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by CornedBee
    You'd use return whenever there's no compelling reason to use exit(). In C++ in particular, note that exit() does not return and objects on the stack of main() won't get destroyed.
    good argument but bad example.


    from the man page of exit

    `exit' does two kinds of cleanup before ending execution of your
    program. First, it calls all application-defined cleanup functions you
    have enrolled with `atexit'. Second, files and streams are cleaned up:
    any pending output is delivered to the host system, each open file or
    stream is closed, and files created by `tmpfile' are deleted.


    so it wont be a problem for files and streams but it would for other user defined classes. No destructors will be called although that wont be a problem in C

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then it seems like Borland doesn't comply with this bit of the standard, because as the OP's try showed, it really didn't write the file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    First, it calls all application-defined cleanup functions you
    have enrolled with `atexit'. Second, files and streams are cleaned up:
    any pending output is delivered to the host system, each open file or
    stream is closed, and files created by `tmpfile' are deleted.
    if i understand you correctly the first prog should have written to the "out.txt"file? and because it did not it is a problem with borlands compiler.

    ok i know return is a statement and not a function but it is easyer to think of it as a function because there are values assocated with it and not a variable identifier. meow bad.

    i have never exited from a function. this may be a bad habit i have but i always return to main to finish the prog.... another bad habit is not writing return 0; in main().

    ok i learned thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM