In C, exit() terminates the calling process without executing the rest code which is after the exit() function.
Example:-
// C program to illustrate exit() function. #include <stdio.h> #include <stdlib.h> int main(void) { printf("START"); exit(0); // The program is terminated here // This line is not printed printf("End of program"); } |
Output:
START
Now the question is that if we have exit() function then why C11 standard introduced _Exit()? Actually exit() function performs some cleaning before termination of the program like connection termination, buffer flushes etc. The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example it does not execute functions registered with atexit.
Syntax:
// Here the exit_code represent the exit status // of the program which can be 0 or non-zero. // The _Exit() function returns nothing. void _Exit(int exit_code);
// C++ program to demonstrate use of _Exit() #include <stdio.h> #include <stdlib.h> int main(void) { int exit_code = 10; printf("Termination using _Exit"); _Exit(exit_code); } |
Output:
Showing difference through programs:
// A C++ program to show difference // between exit() and _Exit() #include<bits/stdc++.h> using namespace std; void fun(void) { cout << "Exiting"; } int main() { atexit(fun); exit(10); } |
Output
Exiting
If we replace exit with _Exit(), then nothing is printed.
// A C++ program to show difference // between exit() and _Exit() #include<bits/stdc++.h> using namespace std; void fun(void) { cout << "Exiting"; } int main() { atexit(fun); _Exit(10); } |
Output
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, 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.
Recommended Posts:
- exit(0) vs exit(1) in C/C++ with Examples
- exit(), abort() and assert()
- return statement vs exit() in main()
- Exit status of a child process in Linux
- Java System.exit(0) vs C++ return 0
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Similarities and Differences between Ruby and C++
- Generate an array of given size with equal count and sum of odd and even numbers
- Similarities and Difference between Java and C++
- 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
- What are the differences between bitwise and logical AND operators in C/C++?
- Change/add only one character and print '*' exactly 20 times
Improved By : Vishal Vishwakarma


