views:

2640

answers:

6

The getche() function doesn't terminate the program properly, so I want to try exit(int status) function. How does it work in Turbo C++ programming language? I cannot understand the explanation in related help modules and I seek for a better explanation... E.g. what does function's parameter consist of? Thanks in advance!

+1  A: 

The parameter is an integer status code that is passed back to the invoking shell as the exit status of the process. Exit itself should exit the process an clean up any open resources that your process is using.

tvanfosson
+3  A: 

Not 100% sure what you mean by this. The parameter to the exit function (the "int status") is a number that gets returned to the shell. Traditionally, this is zero if your program was successful and >0 if the program failed for some reason. The function will clean up various things and then exit your program.

DaedalusFall
+1  A: 

The parameter to Exit() is an int that indicates the reason or status of the exiting process.

Boy, that didn't sound good. Let me try again: It's a value that is passed back to either the starting process (the process that used CreateProcess() or ShellExecute() to launch the one that's exiting), or, in the case of a console app a value to the command shell that can be accessed via ERRORLEVEL.

It's typical to set status = 0 if you're exiting normally, another value (that can have meaning if you want to the process that receives it) to indicate errors or problems.

Ken White
+2  A: 

Well. Functions like getch.. usually get a character from the keyboard or standard input. They are sometimes used at the end of programs like this

int main() {
    // do many stuff...
    // ...
    getch(); 
}

The getch/getche (i don't know what the e stands for in turbo-c++) are then used to give the user the change to see the output of the program, before the terminal windows closes (usually that happens in windows). Note that there is a portable function called getchar in C and C++ that does also do that job (waits for an enter in addition, but that won't cause a harm here).

But it is not used to terminate the program. After a key is pressed, control continues and then after main finished, the program exists. C++ and recent C versions insert a return 0; implicitly after the last statement of the main function (0 stands for "succesful"). This means your main function returns a value of 0 back to the OS. But you can return other values if you write the return explicitly and put another value there. That value is what exit expects. It terminates your program, and returns the given value back to the OS.

int main() {
    // some stuff...
    exit(42);
    // other stuff (note: never reached!)
}

That program will return a value of 42 to the OS. Normally you just return 42; there and it has the same effect (*).


(*) Well, not entirely: If you have local variables, the destructor of those are not called if you use exit. But they are cleanly destructed and destructors are called when you use return n;. Therefor, prefer return n; in main when you can. exit called in other functions than your main will terminate your program too, so it can be required to use that instead, because return there will just return from those specific functions and do not at all terminate the program.

Johannes Schaub - litb
99 times out of 100 the 'destructor' is about freeing memory and closing file handles; things which happen as part of exit anyway. If you really must destruct everything properly register an atexit handler and clean up the few things that really need it.
Adam Hawes
you are completely right i think (i mostly mentioned it so he just basically knows about that). but if we have the chance, why not do it "right" from the start on :)
Johannes Schaub - litb
A: 

The parameter is an integer status code that is passed back to the invoking shell as the exit status of the process. Exit itself should exit the process an clean up any open resources that your process is using.

A: 

More Info on "exit" Function

"exit" function Terminates the process normally, performing the regular cleanup for terminating processes.

First, all functions registered by calls to "atexit" are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.

The value supplied as an argument to exit is returned to the operating system ( the host environment) as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully.

Hope this clears your doubt.

lakshmanaraj