Exit() Function - Control Structures

What is exit() function in C++ Control Structures?
How to terminate program execution in C++?

Explanation

The exit() function is used to terminate program execution and to return to the operating system. The return code "0" exits a program without any error message, but other codes indicate that the system can handle the error messages.

Syntax:


void exit(int return_code);

Example :



#include <iostream.h>
#include <stdlib.h>
void main()
{
int j;
cout << "Enter the bit value 0,1::" << '\n';
cin >> j;
switch( j)
{
case 0:
cout << "Bit value is 0" << '\n';
break;
case 1:
cout << "Bit value is 1" << '\n';
break;
default:
exit(0);
}
}

Result :

Enter the bit value 0,1::3

In the above example, the control structures exit() function is used to terminate program execution of a "switch" statement if the input value is other than 0 or 1.

C++ Tutorial


Ask Questions

Ask Question