Today we’ll learn about exit() in C++. We know we can break out of loops using the built-in break
function in C++. Similarly, we can also break out of a whole C++ program using the exit()
function.
Think of a situation where you are to come to a conclusion in your program. You get the result and conclude something before the whole program has been compiled or evaluated.
How do you terminate the program as soon as you get your required information or result?
The answer to the above question is using the built-in exit()
function in C++. So let us take a closer look at the function and how it works.
Theoretically, the exit()
function in C++ causes the respective program to terminate as soon as the function is encountered, no matter where it appears in the program listing. The function has been defined under the stdlib.h header file, which must be included while using the exit() function.
The syntax for using the exit()
function is given below,
exit( exit_value );
Here, exit_value
is the value passed to the Operating system after the successful termination of the program. This value can be tested in batch files where ERROR LEVEL gives us the return value provided by the exit() function. Generally, the value 0 indicates a successful termination and any other number indicates some error.
Remember, the function exit()
never returns any value. It terminates the process and performs the regular cleanup for terminating programs.
Also, automatic storage objects are not destroyed by calling this function in C++.
Look at the example below carefully:
#include<iostream>
using namespace std;
int main()
{
int i;
cout<<"Enter a non-zero value: "; //user input
cin>>i;
if(i) // checks whether the user input is non-zero or not
{
cout<<"Valid input.\n";
}
else
{
cout<<"ERROR!"; //the program exists if the value is 0
exit(0);
}
cout<<"The input was : "<<i;
}
Output:
Enter a non-zero value: 0
ERROR!
exit()
function and terminates the program.Now let us look at another example where we try to determine whether a number is prime or not.
The program below illustrates the use of exit()
function.
#include<iostream>
using namespace std;
int main()
{
int i,num;
cout<<"Enter the number : ";
cin>>num;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
cout<<"\nNot a prime number!";
exit(0);
}
}
cout<<"\nIt is a prime number!";
return 0;
}
Output:
Further for the above code,
num
is prime or not.exit()
function,In this tutorial, we discussed the working as well as the usage of the built-in exit()
function in C++. It is widely used to terminate the execution of a program.
For any questions comment below.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.