logo

وظيفة الخروج () في C

ال وظيفة الخروج (). يتم استخدامه لإنهاء عملية أو استدعاء وظيفة على الفور في البرنامج. وهذا يعني أن أي ملف مفتوح أو وظيفة تابعة للعملية يتم إغلاقها فورًا عند حدوث وظيفة الخروج () في البرنامج. وظيفة الخروج () هي وظيفة المكتبة القياسية للغة C، والتي تم تعريفها في ملف stdlib.h الملف الاساسي. لذلك يمكننا القول إنها الوظيفة التي تنهي البرنامج الحالي بالقوة وتنقل التحكم إلى نظام التشغيل للخروج من البرنامج. تحدد وظيفة الخروج (0) انتهاء البرنامج دون أي رسالة خطأ، ثم تحدد وظيفة الخروج (1) أن البرنامج ينهي عملية التنفيذ بالقوة.

وظيفة الخروج () في C

النقاط المهمة لوظيفة الخروج ().

فيما يلي النقاط الرئيسية لوظيفة الخروج في برمجة C كما يلي:

  1. يجب علينا تضمين ملف الرأس stdlib.h أثناء استخدام وظيفة الخروج ().
  2. يتم استخدامه لإنهاء التنفيذ العادي للبرنامج أثناء مواجهة وظيفة الخروج ().
  3. تستدعي وظيفة الخروج () وظيفة atexit () المسجلة بالترتيب العكسي لتسجيلها.
  4. يمكننا استخدام وظيفة الخروج () لمسح أو تنظيف جميع بيانات الدفق المفتوح مثل القراءة أو الكتابة باستخدام البيانات المخزنة مؤقتًا غير المكتوبة.
  5. لقد أغلق جميع الملفات المفتوحة المرتبطة بأحد الوالدين أو وظيفة أو ملف آخر ويمكنه إزالة جميع الملفات التي تم إنشاؤها بواسطة وظيفة tmpfile.
  6. يكون سلوك البرنامج غير محدد إذا قام المستخدم باستدعاء وظيفة الخروج أكثر من مرة أو استدعاء وظيفة الخروج والخروج السريع.
  7. يتم تصنيف وظيفة الخروج إلى قسمين: خروج (0) وخروج (1).

بناء جملة وظيفة الخروج ().

 void exit ( int status); 

ال مخرج() الدالة ليس لها نوع إرجاع.

طريقة التبديل جافا

الحالة الدولية: إنه يمثل قيمة حالة وظيفة الخروج التي تم إرجاعها إلى العملية الأصلية.

المثال 1: برنامج يستخدم الدالة exit() في الحلقة for

scan.nextstring java

لنقم بإنشاء برنامج لتوضيح وظيفة الخروج (0) لإنهاء العملية بشكل طبيعي في لغة البرمجة C.

 #include #include int main () { // declaration of the variables int i, num; printf ( &apos; Enter the last number: &apos;); scanf ( &apos; %d&apos;, &amp;num); for ( i = 1; i<num; 0 6 i++) { use if statement to check the condition ( i="=" ) * exit () with passing argument show termination of program without any error message. exit(0); else printf (' 
 number is %d', i); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the last number: 10 Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 </pre> <h3>There are two types of exit status in C</h3> <p>Following are the types of the exit function in the C programming language, as follows:</p> <ol class="points"> <li>EXIT_ SUCCESS</li> <li>EXIT_FAILURE</li> </ol> <p> <strong>EXIT_SUCCESS</strong> : The EXIT_ SUCCESS is the exit() function type, which is represented by the exit(0) statement. Where the &apos;0&apos; represents the successful termination of the program without any error, or programming failure occurs during the program&apos;s execution.</p> <p> <strong>Syntax of the EXIT SUCCESS</strong> </p> <pre> exit (EXIT_SUCCESS); </pre> <p> <strong>Example 1: Program to demonstrate the usage of the EXIT_SUCCESS or exit(0) function</strong> </p> <p>Let&apos;s create a simple program to demonstrate the working of the exit(0) function in C programming.</p> <pre> #include #include int main () { printf ( &apos; Start the execution of the program. 
&apos;); printf (&apos; Exit from the program. 
 &apos;); // use exit (0) function to successfully execute the program exit (0); printf ( &apos;Terminate the execution of the program.
 &apos;); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Start the execution of the program. Exit from the program. </pre> <p> <strong>Example 2: Program to use the EXIT_SUCCESS macro in the exit() function</strong> </p> <p>Let&apos;s create a C program to validate the whether the character is presents or not.</p> <pre> #include #include int main () { // declaration of the character type variable char ch; printf(&apos; Enter the character: &apos;); scanf (&apos; %c&apos;, &amp;ch); // use if statement to check the condition if ( ch == &apos;Y&apos;) { printf(&apos; Great, you did it. &apos;); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (&apos; You entered wrong character!! &apos;); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the character: Y Great, you did it. </pre> <p> <strong>EXIT_FAILURE</strong> : The EXIT_FAILURE is the macro of the exit() function to abnormally execute and terminate the program. The EXIT_FAILURE is also represented as the exit(1) function. Whether the &apos;1&apos; represents the abnormally terminates the program and transfer the control to the operating system.</p> <p> <strong>Syntax of the EXIT_FAILURE</strong> </p> <pre> exit (EXIT_FAILURE); </pre> <p> <strong>Example 1: Let&apos;s create a program to use the EXIT_FAILURE or exit(1) function</strong> </p> <pre> #include #include int main () { int num1, num2; printf (&apos; Enter the num1: &apos;); scanf (&apos;%d&apos;, &amp;num1); printf (&apos; 
 Enter the num2: &apos;); scanf (&apos;%d&apos;, &amp;num2); if (num2 == 0) { printf (&apos; 
 Dividend cannot be zero. &apos;); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (&apos; %d / %d : %f&apos;, num1, num2, num3); // use the EXIT_SUCCESS exit(0); } </pre> <p> <strong>Output</strong> </p> <pre> Enter the num1: 20 Enter the num2: 6 20 / 6 : 3.333333 2nd Run Enter the num1: 20 Enter the num2: 6 Dividend cannot be zero </pre> <p> <strong>Example 2: Let&apos;s create another program to use the EXIT_FAILURE to terminate the C program.</strong> </p> <pre> #include #include int main () { // declare the data type of a file FILE *fptr = fopen ( &apos;javatpoint.txt&apos;, &apos;r&apos; ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, &apos;Unable to open the defined file 
&apos; ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( &apos; Normal termination of the program. &apos;); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Unable to open the defined file. </pre> <hr></num;>

هناك نوعان من حالة الخروج في C

فيما يلي أنواع دالة الخروج في لغة البرمجة C، كما يلي:

  1. خروج_ النجاح
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_ SUCCESS هو نوع الدالة exit()، والذي يتم تمثيله بواسطة عبارةexit(0). حيث يمثل الرقم '0' الإنهاء الناجح للبرنامج دون أي خطأ أو حدوث فشل في البرمجة أثناء تنفيذ البرنامج.

بناء جملة نجاح الخروج

 exit (EXIT_SUCCESS); 

مثال 1: برنامج لتوضيح استخدام الدالة EXIT_SUCCESS أو الخروج (0).

لنقم بإنشاء برنامج بسيط لتوضيح عمل وظيفة الخروج (0) في برمجة C.

 #include #include int main () { printf ( &apos; Start the execution of the program. 
&apos;); printf (&apos; Exit from the program. 
 &apos;); // use exit (0) function to successfully execute the program exit (0); printf ( &apos;Terminate the execution of the program.
 &apos;); return 0; } 

انتاج |

 Start the execution of the program. Exit from the program. 

المثال 2: برنامج لاستخدام الماكرو EXIT_SUCCESS في وظيفة الخروج ().

لنقم بإنشاء برنامج C للتحقق من وجود الشخصية أم لا.

التفاف النص المغلق
 #include #include int main () { // declaration of the character type variable char ch; printf(&apos; Enter the character: &apos;); scanf (&apos; %c&apos;, &amp;ch); // use if statement to check the condition if ( ch == &apos;Y&apos;) { printf(&apos; Great, you did it. &apos;); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (&apos; You entered wrong character!! &apos;); } return 0; } 

انتاج |

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE هو الماكرو الخاص بوظيفة الخروج () لتنفيذ البرنامج وإنهائه بشكل غير طبيعي. يتم أيضًا تمثيل EXIT_FAILURE كوظيفة خروج (1). ما إذا كان الرقم '1' يمثل إنهاء البرنامج بشكل غير طبيعي ونقل التحكم إلى نظام التشغيل.

جافا تعدد المواضيع

بناء جملة EXIT_FAILURE

 exit (EXIT_FAILURE); 

المثال 1: لنقم بإنشاء برنامج لاستخدام الدالة EXIT_FAILURE أو الدالة exit(1).

 #include #include int main () { int num1, num2; printf (&apos; Enter the num1: &apos;); scanf (&apos;%d&apos;, &amp;num1); printf (&apos; 
 Enter the num2: &apos;); scanf (&apos;%d&apos;, &amp;num2); if (num2 == 0) { printf (&apos; 
 Dividend cannot be zero. &apos;); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (&apos; %d / %d : %f&apos;, num1, num2, num3); // use the EXIT_SUCCESS exit(0); } 

انتاج |

 Enter the num1: 20 Enter the num2: 6 20 / 6 : 3.333333 2nd Run Enter the num1: 20 Enter the num2: 6 Dividend cannot be zero 

مثال 2: لنقم بإنشاء برنامج آخر لاستخدام EXIT_FAILURE لإنهاء برنامج C.

 #include #include int main () { // declare the data type of a file FILE *fptr = fopen ( &apos;javatpoint.txt&apos;, &apos;r&apos; ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, &apos;Unable to open the defined file 
&apos; ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( &apos; Normal termination of the program. &apos;); return 0; } 

انتاج |

 Unable to open the defined file.