C Board  

Go Back   C Board > General Programming Boards > C Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 08-15-2001, 11:21 AM   #1
Badly Drawn Boy
 
ethic's Avatar
 
Join Date: Aug 2001
Location: Salt Lake City, Utah
Posts: 1,201
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.
ethic is offline  
Old 08-15-2001, 11:44 AM   #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.
mix0matt is offline  
Old 08-15-2001, 11:54 AM   #3
Mayor of Awesometown
 
Govtcheez's Avatar
 
Join Date: Aug 2001
Location: MI
Posts: 8,826
So why would you use exit instead of return?
__________________
-Govtcheez
govtcheez03@hotmail.com
Govtcheez is offline  
Old 08-15-2001, 11:57 AM   #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.
Nick is offline  
Old 08-15-2001, 12:22 PM   #5
junior member
 
mix0matt's Avatar
 
Join Date: Aug 2001
Posts: 144
Quote:
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.
mix0matt is offline  
Old 08-15-2001, 02:34 PM   #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
@licomb is offline  
Old 08-15-2001, 05:46 PM   #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
Witch_King is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Very quick math question jverkoey General Discussions 8 10-26-2005 11:05 PM
very quick question. Unregistered C++ Programming 7 07-24-2002 03:48 AM
quick question Unregistered C++ Programming 5 07-22-2002 04:44 AM
Quick Question Regarding Pointers charash C++ Programming 4 05-04-2002 11:04 AM
Quick Question on File Names and Directories Kyoto Oshiro C++ Programming 4 03-29-2002 02:54 AM


All times are GMT -6. The time now is 04:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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