![]() |
Calling exit(???) functions
Hi 2 All
I have a slight problem with the exit() functions used in C. I would like to know what the difference is between exit(0), exit(1), and exit(-1). These are some of the exit functions I have encountered so far. All input will be appreciated. |
Re: Calling exit(???) functions
They're the same function, just passed a different value as the parameter. They'll cause your program to terminate, and it will return the passed value back to the operating system. Convention dictates that a return value of 0 means that all went well and anything else is an error code.
|
Re: Calling exit(???) functions
exit(int status) is defined by standard to close open files, stop the executable image from running and to return the value of status to the calling program. In MS this could be windows explorer, a DOS sub-shell, or parent program. In unix it could be a parent program or the shell. EXIT_SUCCESS and EXIT_FAILURE are macros that are defined in the standard.
What you put in a "special" status depends on what the "receiver" of the status wants or can deal with, so it is implmentation dependent. Read the manual for your compiler. As Jimbo mentioned there are some conventions. There are also some "proposed" exit values that you will see from time to time - an example is sysexits.h which defines macros like EX_USER. This is why you will see different error returns from different programs and they may have special meaning. You can google for sysexits.h and look at the include file if you want. |
Re: Calling exit(???) functions
Thanks
It makes alot more sense now. Just wondering... Why does the C BOOK use EXIT_SUCCES and not return(0)?All input much appreciated. Thanks BstrucT |
Re: Calling exit(???) functions
Using
EXIT_SUCCESS makes it clear what the intended result is. The principle here is that named constants are clearer than numbers in most cases. |
Re: Calling exit(???) functions
Quote:
exit(EXIT_SUCCESS); as I can now make out easier where it is, by examining the source code, especially since I used switch() statements in my little calculator app.I definitely see the point in using it. Thanks again. |
Re: Calling exit(???) functions
I don't understand why you are using exit(). Why call a function when you can simply return to leave the program?
|
Re: Calling exit(???) functions
exit() is useful in error handling... if you don't want the function to continue working due to some error and you want to stop the execution of the application, exit is appropriate.
|
Re: Calling exit(???) functions
Hi Guys
I used the exit(EXIT_SUCCESS) when I want the program to exit completely and stop execution.I wrote a little calculator app(very basic) that uses a menu system I implemented by means of switch(). I used return() to get the user back to the main menu, and used exit(EXIT_SUCCESS) to exit the program completely.I have attached the source code because I would like to know if this is the correct usage of these functions. This is basically the same program from my previous post "BEGINNER C CALCULATOR PROBLEM", only difference I took the advise I got from the experts here, and have revised it. :
/* C - Calculator Revised. */ |
Re: Calling exit(???) functions
It is often considered poor style to call main() recursively, and it is actually illegal in C++. Technically, it is a VERY bad idea to do that in response to input (from either a user or a file) as the program has no way of controlling the depth of recursion, and deep levels of recursion can consume resources (eg stack space) available to the program. Users of your program will normally expect it can happily loop forever if they enter appropriate input; they will not expect it to consume system resources (eg memory) or to - eventually - terminate abnormally when it consumes all available resources.
Better to set up looping constructs within main() so you can better understand what the program does, rather than calling main() recursively everywhere. A side effect of that is that "return status;" and "exit(status);" will have the same effect (at least, within main()). You are referring to zero values as NULLs in prompts to the user. Not technically incorrect, but those words only makes sense to the user of your program if that user is a C/C++ programmer. If you want to test if the user has hit <RETURN> checking a field read in by scanf() is not the way to do it, because that function will wait for non-space input. Your division() and subtraction() functions will produce results that will surprise the user of your program on occasion. Users tend to complain when programs behave in surprising ways. I fail to see the point of using function pointers in your code, but there's no real harm I guess. |
| All times are GMT -5. The time now is 3:43 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC