which function is used to terminate a program in c
exit() Function in C Language in Hindi
Purpose of exit( ) Function in C Language in Hindi
What is exit() function?
C language में exit() function एक predefined function होता है जो program को तुरंत terminate कर देता है, चाहे वह किसी भी जगह से call किया गया हो। जब भी हम किसी program के बीच में ऐसा point पहुँचते हैं जहाँ हमें program को तुरंत रोक देना हो, वहाँ हम exit() का उपयोग करते हैं।
Header File Requirement
exit() function का प्रयोग करने के लिए हमें #include header file को अपने program में include करना होता है। क्योंकि यह function stdlib.h में define किया गया है।
Purpose (उद्देश्य)
- Program को किसी भी point से तुरंत समाप्त करना।
- Error या unexpected condition आने पर program को safely terminate करना।
- Operating System को एक exit status code return करना।
Use Case Example
मान लीजिए कोई file open नहीं हो रही है या memory allocate नहीं हो रही, तो ऐसे cases में हम error message दिखा कर exit() के जरिए program को वहीं खत्म कर सकते हैं।
#include
#include
int main() {
FILE *file = fopen("demo.txt", "r");
if (file == NULL) {
printf("File open नहीं हो पाई।\\n");
exit(1);
}
printf("File successfully खुल गई।\\n");
fclose(file);
return 0;
}
Syntax and Use of exit( ) in Programs in Hindi
Syntax
void exit(int status);
यहाँ status एक integer value होती है जिसे हम return करते हैं Operating System को, जिससे यह पता चलता है कि program normal तरीके से समाप्त हुआ या error के कारण।
Common Status Codes
exit(0);→ Program सफलतापूर्वक terminate हुआ।exit(1);→ Program में कोई general error आया।exit(EXIT_SUCCESS);→ सफल termination के लिए standard macro।exit(EXIT_FAILURE);→ असफल termination के लिए standard macro।
Program Example with exit(0)
#include
#include
int main() {
printf("Program शुरू हो गया।\\n");
exit(0);
printf("यह line कभी execute नहीं होगी।\\n");
}
ऊपर दिए गए program में, exit(0); के बाद की कोई भी statement execute नहीं होगी।
Difference between return and exit( ) in Hindi
return और exit() में अंतर
| बिंदु | return | exit() |
|---|---|---|
| परिभाषा | Function से बाहर आने के लिए | पूरे program को terminate करने के लिए |
| कहाँ उपयोग होता है | सिर्फ function के अंदर | कहीं भी, main() या किसी function के अंदर |
| Program का प्रभाव | केवल function terminate होता है | पूरा program terminate हो जाता है |
| Header file की आवश्यकता | नहीं | stdlib.h |
| Exit Status | main() से ही भेज सकते हैं | किसी भी जगह से भेज सकते हैं |
Example for return
#include
int display() {
printf("Display function से return किया गया।\\n");
return 0;
}
int main() {
display();
printf("Main function पूरा हुआ।\\n");
return 0;
}
Example for exit()
#include
#include
int check() {
printf("Error आया, program को अब खत्म करेंगे।\\n");
exit(1);
}
int main() {
check();
printf("यह line कभी नहीं चलेगी।\\n");
return 0;
}
exit( ) Function with Exit Codes in Hindi
Exit Codes का महत्व
जब कोई program terminate होता है, तब वह Operating System को एक value return करता है जिसे exit code कहते हैं। इस exit code से यह पता चलता है कि program सफलतापूर्वक समाप्त हुआ या किसी error के कारण terminate हुआ।
Standard Exit Codes
exit(0)याEXIT_SUCCESS→ सब कुछ ठीक था।exit(1),exit(-1)याEXIT_FAILURE→ कोई न कोई error आया।
Macro Constants
- EXIT_SUCCESS – यह macro
stdlib.hमें define होता है, और successful termination को दर्शाता है। - EXIT_FAILURE – यह unsuccessful termination को दर्शाता है।
Example using EXIT_SUCCESS
#include
#include
int main() {
printf("Program सफलतापूर्वक पूरा हुआ।\\n");
exit(EXIT_SUCCESS);
}
Example using EXIT_FAILURE
#include
#include
int main() {
printf("कोई error आया है।\\n");
exit(EXIT_FAILURE);
}
Operating System या calling process इन exit codes का उपयोग करके यह जान सकते हैं कि program successfully terminate हुआ या नहीं। यह खासकर scripts और automated tasks में बहुत काम आता है।
FAQs
void exit(int status); इसमें status एक integer value होती है जो operating system को return की जाती है यह बताने के लिए कि program success से समाप्त हुआ या failure से।
exit(0)याEXIT_SUCCESS– successful termination के लिए।exit(1),exit(-1)याEXIT_FAILURE– error या failure के लिए।