![]() |
| | #1 |
| Badly Drawn Boy Join Date: Aug 2001 Location: Salt Lake City, Utah
Posts: 1,201
| Quick question: exit(); 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 |
| | #2 |
| junior member 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 |
| | #3 |
| Mayor of Awesometown Join Date: Aug 2001 Location: MI
Posts: 8,826
| So why would you use exit instead of return? |
| Govtcheez is offline |
| | #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 |
| | #5 | |
| junior member Join Date: Aug 2001
Posts: 144
| Quote:
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 |
| | #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 |
| | #7 |
| Anti-Terrorist Join Date: Aug 2001 Location: mming, Game DevelopmentCSR
>& |