Home »
C++ programming »
C++ Reference »
C++ <cstdlib> header
exit() Function with Example in C++
C++ exit() function: Here, we are going to learn about the exit() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 28, 2020
C++ exit() function
exit() function is a library function of cstdlib header. It is used to terminate the calling process, it accepts a status code/value (EXIT_SUCCESS or EXIT_FAILURE) and terminates the process. For example – while working with the files, if we are opening a file and file does not exist – in this case, we can terminate the process by using the exit() function.
Syntax of exit() function:
C++11:
void exit (int status);
Parameter(s):
- status – represents the exit status code. 0 or EXIT_SUCCESS indicates the success i.e. the operation is successful, EXIT_FAILURE indicates the failure i.e. the operation is not successfully executed.
Return value:
The return type of this function is void, It does not return anything.
Example:
Function call:
exit(EXIT_FAILURE);
//or
exit(EXIT_SUCCESS);
C++ code to demonstrate the example of exit() function
// C++ code to demonstrate the example of
// exit() function
#include <iostream>
#include <cstdlib>
using namespace std;
// main() section
int main()
{
float n; // numerator
float d; // denominator
float result;
cout << "Enter the value of numerator : ";
cin >> n;
cout << "Enter the value of denominator: ";
cin >> d;
if (d == 0) {
cout << "Value of denominator should not be 0..." << endl;
exit(EXIT_FAILURE);
}
cout << n << " / " << d << " = " << (n / d) << endl;
return 0;
}
Output
RUN 1:
Enter the value of numerator : 10
Enter the value of denominator: 0
Value of denominator should not be 0...
RUN 2:
Enter the value of numerator : 10
Enter the value of denominator: 3
10 / 3 = 3.33333
Reference: C++ exit() function