How to terminate program in C++

Hey, guys… Today we are going to learn how to terminate a program in C++. We can terminate a program in C++ in different methods that are listed below:

  1. By using exit() function
  2.  By using abort() or _Exit() function
  3.  By using the return statement in main()

Before discussing different methods let’s discuss the cleanup process which is followed at the time of terminating a program.

Cleanup process and atexit() function

Cleanup process: This process normally occurs before the termination of a program takes place. It is also known as C++ run time termination processing. The cleanup process includes:

  • Calling of the global objects’ and the static objects’ destructors.
  • Calling of functions registered successfully with atexit() function.
  •  Connection termination and flushing the buffer. Temporary files are also deleted.
  •  Returning the control back to the host i.e Operating System.

atexit() function: It registers the functions to be called during the C++ run time termination of a program. atexit() takes the function to be registered as the argument and returns zero on successful registration. Header file for atexit() function is <stdlib.h> or <cstdlib>.

#include <iostream>
#include <stdlib.h>

using namespace std;
// creating a function named Quit
void Quit()
{
    cout<<"Thanks for choosing our program,good bye"<<endl;
}
int main()
{
    //registering Quit with atexit()
    int a=atexit(Quit);
    if (a != 0)
    cout<<"Registration failed";
    exit(0);
    return 0;
}

Output:

Thanks for choosing our program,good bye

1. Terminating a program using exit() function

exit() function terminates a program in C++. The header file for exit function is <stdlib.h>or <cstdlib>. It terminates the program normally i.e. It performs the cleanup process ( also known as C++ runtime termination). The code written after exit() does not get executed in a program. The argument that we pass to exit() function ( usually 0 or 1) is returned as the exit/return code by the exit() function.

#include <iostream>
#include <stdlib.h>

using namespace std;
// creating a function named Quit
void Quit()
{
    cout<<"Thanks for choosing our program,good bye"<<endl;
}
int main()
{
    
    string name;
    //registering Quit with atexit()
    int a=atexit(Quit);
    if (a != 0)
    cout<<"Registration failed";
    //Calling the exit() function
    exit(0);
    cout<<"enter your name ";
    cin>>name;
    cout<<"Hello "<<name;
    return 0;
}

Output:

Thanks for choosing our program, good bye

2. Terminating a program using _Exit() or abort() function

Both abort() and _Exit()  are similar functions. Both abort() and _Exit() terminate a program immediately. Both of these functions are available in the header file <stdlib.h> or <cstdlib>_Exit() and abort() terminate the program immediately without performing any cleanup process like exit(). Thus the functions linked with atexit() are not executed. The code which is written after _Exit() / abort() does not get executed in a program.

The only difference between _Exit() and abort() is that an argument (usually 0 or 1) is passed to _Exit() like exit() whereas no argument is passed to abort().

Demonstrating _Exit() function:

#include <iostream>
#include <stdlib.h>

using namespace std;
// creating a function named Quit
void Quit()
{
    cout<<"Thanks for choosing our program, good bye"<<endl;
}
int main()
{
    
    string name;
    //registering Quit with atexit()
    int a=atexit(Quit);
    if (a != 0)
    cout<<"Registration failed";
    //Calling the _Exit() function
    _Exit(0);
    cout<<"enter your name ";
    cin>>name;
    cout<<"Hello "<<name;
    return 0;
}

Demonstrating abort() function:

#include <iostream>
#include <stdlib.h>

using namespace std;
// creating a function named Quit
void Quit()
{
    cout<<"Thanks for choosing our program, good bye"<<endl;
}
int main()
{
    
    string name;
    //registering Quit with atexit()
    int a=atexit(Quit);
    if (a != 0)
    cout<<"Registration failed";
    //Calling the abort() function
    abort();
    cout<<"enter your name ";
    cin>>name;
    cout<<"Hello "<<name;
    return 0;
}

Output:

Aborted (core dumped)

3. Terminating a program using return statement in main()

Using the return statement in main() is similar to exit() function discussed above. The return statement returns back the control from a function and code written after the return statement is not executed. Thus return statement written in main() is used to terminate a program as it returns the control back from the main() function. Its syntax is:

return data_of_return_datatype

Like exit it terminates, the program normally i.e It performs the cleanup process ( also known as C++ runtime termination). Return data is similar to the argument passed to exit() and acts as return/exit code for the program.

#include <iostream>
#include <stdlib.h>

using namespace std;
// creating a function named Quit
void Quit()
{
    cout<<"Thanks for choosing our program, good bye"<<endl;
}
int main()
{
    
    string name;
    //registering Quit with atexit()
    int a=atexit(Quit);
    if (a != 0)
    cout<<"Registration failed";
    /* Using return statement in main() to terminate the program */
    return 1;
    cout<<"enter your name ";
    cin>>name;
    cout<<"Hello "<<name;

}

Output:

Thanks for choosing our program, good bye

Also, do refer:

Leave a Reply

Your email address will not be published. Required fields are marked *