exit() terminates the process normally.
status: Status value returned to the parent process. Generally, a status value of 0 or EXIT_SUCCESS indicates success, and any other value or the constant EXIT_FAILURE is used to indicate an error. exit() performs following operations.
* Flushes unwritten buffered data.
* Closes all open files.
* Removes temporary files.
* Returns an integer exit status to the operating system.
The C standard atexit() function can be used to customize exit() to perform additional actions at program termination.
Example use of exit.
When exit() is called, any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal.
The mystery behind exit() is that it takes only integer args in the range 0 – 255 . Out of range exit values can result in unexpected exit codes. An exit value greater than 255 returns an exit code modulo 256.
For example, exit 9999 gives an exit code of 15 i.e. (9999 % 256 = 15).
Below is the C implementation to illustrate the above fact:
Exit code: 15
Note that the above code may not work with online compiler as fork() is disabled.
Explanation: It is effect of 8-bit integer overflow. After 255 (all 8 bits set) comes 0.
So the output is “exit code modulo 256”. The output above is actually the modulo of the value 9999 and 256 i.e. 15.
Unlike exit() function, abort() may not close files that are open. It may also not delete temporary files and may not flush stream buffer. Also, it does not call functions registered with atexit().
This function actually terminates the process by raising a SIGABRT signal, and your program can include a handler to intercept this signal (see this).
So programs like below might not write “Geeks for Geeks” to “tempfile.txt”
If we want to make sure that data is written to files and/or buffers are flushed then we should either use exit() or include a signal handler for SIGABRT.
If expression evaluates to 0 (false), then the expression, sourcecode filename, and line number are sent to the standard error, and then abort() function is called. If the identifier NDEBUG (“no debug”) is defined with #define NDEBUG then the macro assert does nothing.
Common error outputting is in the form:
Assertion failed: expression, file filename, line line-number
Related Article :
exit() vs _Exit() in C and C++
This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect in the above article or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- exit(0) vs exit(1) in C/C++ with Examples
- exit() vs _Exit() in C and C++
- return statement vs exit() in main()
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- What are the differences between bitwise and logical AND operators in C/C++?
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Getting System and Process Information Using C Programming and Shell in Linux
- INT_MAX and INT_MIN in C/C++ and Applications
- Difference and Similarities between PHP and C
- Change/add only one character and print '*' exactly 20 times
- Comma in C and C++
- Return values of printf() and scanf() in C/C++
- What is return type of getchar(), fgetc() and getc() ?
- Difference Between malloc() and calloc() with Examples
- How does "void *" differ in C and C++?
- What are the operators that can be and cannot be overloaded in C++?
- Write one line functions for strcat() and strcmp()
- Data type of character constants in C and C++
Improved By : mazhar_mik