Tutorials  /  Linux Basics

The exit() function in C++

Ccentron Redaktion · January 2024 ·4 min read ·Linux Basics, Tutorial

Introduction

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.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Definition of the exit() function in C++

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.

Syntax for the exit() function in C++

The syntax for using the exit() function is given below,

Code
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.

Working of the exit() function in C++

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:

Code
#include
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:

Code
Enter a non-zero value: 0
ERROR!
  • Since the user input for the above code provided is zero(0), the else part is executed for the if-else statement. Further where the compiler encounters the exit() function and terminates the program.
  • Even the print statement below the if-else is not executed since the program has been terminated by the exit() function already.

Now let us look at another example where we try to determine whether a number is prime or not.

Using the exit() function in C++

The program below illustrates the use of exit() function.

Code
#include
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;
}

Further for the above code,

  • Firstly we took a user input for the number. We need to check whether this number, num, is prime or not.
  • After that, we apply a for loop which works from 2 to n/2. This is because we already know that all numbers are divisible by 1 as well as a number is indivisible by numbers above its half.
  • Inside the for loop, we check whether the number is divisible by the loop iterator at that instant. If so, we can directly conclude that the number is not prime and terminate the program using the exit() function,
  • Else, the loop goes on checking. After the execution of the whole loop structure, we declare the number as a prime one.

Conclusion

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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren