exit()
C ++ में कोई keyword नहीं है, यह एक function है जो पहले से ही C++ library में defined है क्योंकि इसका व्यवहार एक control – statement की तरह ही होता है इसलिए हमने इसे यहां defined किया है।
सामान्य रूप से, exit()
function operating system को रिपोर्ट करता है की program ने successful execution किया है या नहीं।
exit()
function program को error -code के साथ समाप्त करता है जहां 0 normal termination को प्रदर्शित करता है और गैर-शून्य abnormal termination को। abnormal termination data -overflow , size exceeded , incomplete file operation आदि से संबंधित है।
program को terminate करने से पहले यह निम्नलिखित operation करता है,
- Terminate the calling process.
- close all files.
- calls registered function (if available).
exit()
function को header file stdlib.h
और process.h
में defined किया गया है। इसलिए, हमें program में इनकी header file (दोनों में से कोई एक ) को declared करना होगा।
इसका syntax नीचे दिया गया है –
exit(error-code);
जहाँ error-code normal या abnormal termination को प्रदर्शित करता है, सामान्य रूप से 0 normal termination अन्य मान abnormal termination को प्रदर्शित करता है।
हालाँकि, हम नंबर्स की जगह हम header file stdilb.h
और process.h
में defined निम्नलिखित दो constant का भी प्रयोग कर सकते हैं-
EXIT_SUCCESS
, represent by zeroEXIT_FAILURE
, represent by none-zero
जैसे,
exit(0) or exit(EXIT_SUCCESS) |
normal program termination को प्रदर्शित करता है। |
exit(1) or exit(EXIT_FAILURE) |
abnormal program termination को प्रदर्शित करता है। |
यहाँ हम एक normal और abnormal termination को perform करेंगे । निम्नलिखित पहले दो उदाहरण abnormal termination हैं,
exit statement flow chart in C++
जैसा कि आप ऊपर दिए गए flow -diagram में देख सकते हैं जब execution exit statement में जाता है। तब program तुरंत terminate हो जाएगा और बाकी के कोड (program में अतिरिक्त statements ) को छोड़ देगा।
आइए यहां एक उदाहरण के साथ समझते हैं –
Example of exit in C++
निम्नलिखित Program में, हम reading -mode (ifstream class ) में एक non – existing file को open करने की कोशिश कर रहे हैं,
#include<process.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
int main(void)
{
ifstream fin;
fin.open("file.txt");
if(!fin) // test for an error on the stream
{
cout<<"File can't open...\n"; // display an error message
getch();
exit(EXIT_FAILURE);
}
else {
cout<<"statement never executed...";
}
fin.close();
}
OUTPUT
File can't open...
Explanation:
Program में हम पढ़ने के लिए एक file open की कोशिश कर रहे हैं, लेकिन कोई storage में file ही exist नहीं है। तो यहाँ एक abnormal termination होगा।
यहाँ एक और उदाहरण है,
यहाँ हम graphics के साथ काम कर रहे हैं, graphics प्रयोग करने से पहले हम जांच करेंगे कि ग्राफिक प्रतिक्रिया दे रहा है या नहीं,
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
void main()
{
int gdriver = DETECT, gmode, error_code;
initgraph(&gdriver, &gmode, "");
error_code = graphresult();
if(error_code != grOk)// check graphic driver responding or not
{
cout<<"Graphics error:\n"<<grapherrormsg(error_code);
getch();
exit(EXIT_FAILURE); // report Execution failure
}
// draw a line
line(0, 0, getmaxx(), getmaxy());
closegraph();
}
OUTPUT
Graphic Error:
Device driver file not found (EGAVGA.BGI)
अब निम्नलिखित programs normal termination के उदाहरण हैं।
निम्नलिखित program में, हम program को normal exit कराते हैं जो user -input पर निर्भर करता है।
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int num1,num2;
for(int i=0; i>=0; i++) //infinite loop
{
cout<<"Enter Two number: ";
cin>>num1>>num2;
if(!num1||!num2) {
cout<<"Program Exiting...";
exit(0); // EXIT_SUCCESS
}
else
cout<<"sum: "<<num1+num2;
getch();
}
}
OUTPUT
Enter number: 3 6
sum 9
Enter number: 2 5
sum 7
Enter number: 0 3
Program Exiting...
Explanation
जैसे ही दो variables में से एक का मान 0 होता है, program normal exit करता है। नीचे flow-diagram उपरोक्त program का प्रदर्शन है-

Example of exit() in a menu-driven program
नीचे दिए गए program में exit()
को एक menu – driven में प्रयोग किया गया है,
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int input();
int input()
{
int value;
cout<<"Enter Degree: ";
cin>>value;
return value;
}
void main()
{
int select,result;
do{
clrscr();
cout<<"1> celsius To Fahrenheit\n";
cout<<"2> Fahrenheit To Celsius\n";
cout<<"3> Celsius To Kelvin\n";
cout<<"4> Kelvin To Celsius\n";
cout<<"5> Fahrenheit To Kelvin\n";
cout<<"0> Exit\n\n";
cin>>select;
switch(select) {
case 1:
result=(input()*9/5)+32;
cout<<result<<"f";
getch();
break;
case 2:
result=(input()-32)*5/9;
cout<<result<<"c";
getch();
break;
case 3:
result=input()+273;
cout<<result<<"k";
getch();
break;
case 4:
result=input()-273;
cout<<result<<"c";
getch();
break;
case 5:
result=(input()+459)*5/9;
cout<<result<<"k";
getch();
break;
case 0: exit(0);
default:
cout<<"Not available..";
}
}while(select!=0);
}
OUTPUT
Enter degree: 212
100c
difference between break and exit() statement in C++ hindi
सरल शब्दों में कहें , एक exit()
function का उपयोग हमेशा एक पुरे program को समाप्त करने के लिए किया जाता है जबकि break केवल single statement को terminate करता है।
break statement |
exit() |
---|---|
syntax: break; |
exit(int error_code); |
break एक keyword है |
जबकि exit() एक Standard Library Function है। |
इसका उपयोग अन्य control statement से बाहर निकलने के लिए किया जाता है। (control statement को terminate करने के लिए , program को नहीं ) | इसका उपयोग program से बाहर निकलने के लिए किया जाता है। (पुरे program को terminate करने के लिए )। |
control statement को समाप्त करना हमेशा एक normal execution होता है। | इसमें दो, normal execution और abnormal execution होता है। |
break के बाद program execution में बना रहता है। |
exit program को terminate करता है। |
break का उपयोग करने से पहले हमें किसी header file की आवश्यकता नहीं है। |
exit() का उपयोग करने से पहले हमें program में इसकी header file , stdlib या process को शामिल करना होता है। |
यहां एक और उदाहरण है जहां इसे function के साथ दिया गया है function with switch statement in C++ जहाँ हम menu में exit विकल्प देते हैं।
previous – goto statement in C++
next- array in C++