Quick question: exit();

This is a discussion on Quick question: exit(); within the C Programming forums, part of the General Programming Boards category; I apologize for asking a super-simple question, but my book says nothing about it...(Well, at least I don't think it ...

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Quick question: exit();

    I apologize for asking a super-simple question, but my
    book says nothing about it...(Well, at least I don't think
    it does. I checked the index, but nothing.)

    Quick question. What does the exit() function do? What
    would it do in this snippet of code:

    Code:
    int main()
    {
      //Question on exit()
    
      FILE *filestuff;
      
      filestuff=fopen("file","w");
      if(filestuff==NULL)
      {
         pritnf("Sorry. \n");
         exit(1);
      }
      return 0;
    }

    I imagine it exits from something. But what? And what
    parameters does it accept? What do they mean?

    Much obliged.
    Staying away from General.

  2. #2
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144

    exit()

    The exit() terminates the program much like the return statement in the main function. It return control to the operating system. It closes open files, flushes output buffers, and calls destructors before control is returned to the operating system. The parameter passed to the function are exit status values which communicate to the operating system the program terminated prematurely or everything happened as expected. For example exit(0) (same as return 0) means the program terminated normally. exit(1) (same as return 1) means the program terminated because of some error ( it could not open a file, allocate memory, etc. ).
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    So why would you use exit instead of return?

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    In that case you could just say

    return EXIT_FAILURE;

    in other cases where your inside a function like

    void f(void)
    {
    exit(EXIT_FAILURE);
    }

    putting a return there would be a syntax error.

  5. #5
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    So why would you use exit instead of return?
    The return statement would work fine if the error occured in main(). exit() can cleanly terminate the program from within any function. Like in Nick's example.

    Interesting though...i've seen a lot of old school and sometimes nonstandard C code that rely completely on the exit() function. no return statement at all...
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Don't forget to include the relevent library

    #include <stdlib.h>

    for the exit function to work!!
    -ali

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Use exit in C and in C++ you can use exception handling instead.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 01:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21