logo

Funkcia exit() v C

The funkciu exit(). sa používa na okamžité ukončenie volania procesu alebo funkcie v programe. Znamená to, že každý otvorený súbor alebo funkcia patriaca do procesu sa zatvorí okamžite, keď sa v programe vyskytne funkcia exit(). Funkcia exit() je štandardná knižničná funkcia jazyka C, ktorá je definovaná v stdlib.h hlavičkový súbor. Môžeme teda povedať, že je to funkcia, ktorá násilne ukončí aktuálny program a prenesie kontrolu na operačný systém, aby program ukončil. Funkcia exit(0) určí, že sa program ukončí bez akéhokoľvek chybového hlásenia, a potom funkcia exit(1) určí, že program násilne ukončí proces vykonávania.

Funkcia exit() v C

Dôležité body funkcie exit().

Nižšie sú uvedené hlavné body funkcie ukončenia v programovaní C takto:

  1. Pri používaní funkcie exit () musíme zahrnúť hlavičkový súbor stdlib.h.
  2. Používa sa na ukončenie normálneho vykonávania programu, keď narazíte na funkciu exit ().
  3. Funkcia exit () volá zaregistrovanú funkciu atexit() v opačnom poradí ako pri ich registrácii.
  4. Môžeme použiť funkciu exit() na vyprázdnenie alebo vyčistenie všetkých otvorených údajov toku, ako je čítanie alebo zápis s nezapísanými údajmi vo vyrovnávacej pamäti.
  5. Zatvoril všetky otvorené súbory spojené s nadradenou alebo inou funkciou alebo súborom a môže odstrániť všetky súbory vytvorené funkciou tmpfile.
  6. Správanie programu nie je definované, ak používateľ zavolá funkciu ukončenia viac ako jedenkrát alebo zavolá funkciu ukončenia a quick_exit.
  7. Funkcia exit je rozdelená do dvoch častí: exit(0) a exit(1).

Syntax funkcie exit().

 void exit ( int status); 

The VÝCHOD() funkcia nemá návratový typ.

poštár

stav int: Predstavuje hodnotu stavu funkcie ukončenia vrátenej rodičovskému procesu.

Príklad 1: Program na použitie funkcie exit() v slučke for

Vytvorme program na demonštráciu funkcie exit (0) pre normálne ukončenie procesu v programovacom jazyku 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;>

V C sú dva typy výstupného stavu

Nasledujú typy funkcie ukončenia v programovacom jazyku C:

  1. EXIT_ SUCCESS
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_ SUCCESS je typ funkcie exit(), ktorý je reprezentovaný príkazom exit(0). Kde '0' predstavuje úspešné ukončenie programu bez akejkoľvek chyby alebo zlyhanie programovania počas vykonávania programu.

v regexe java

Syntax EXIT SUCCESS

 exit (EXIT_SUCCESS); 

Príklad 1: Program na demonštráciu použitia funkcie EXIT_SUCCESS alebo exit(0).

Vytvorme jednoduchý program na demonštráciu fungovania funkcie exit(0) v programovaní v jazyku C.

vek mia Khalifa
 #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; } 

Výkon

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

Príklad 2: Program na použitie makra EXIT_SUCCESS vo funkcii exit().

Vytvorme program C na overenie toho, či je znak prítomný alebo nie.

 #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; } 

Výkon

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE je makro funkcie exit() na abnormálne spustenie a ukončenie programu. EXIT_FAILURE je tiež reprezentovaná ako funkcia exit(1). Či '1' predstavuje abnormálne ukončenie programu a prenos kontroly do operačného systému.

Syntax EXIT_FAILURE

 exit (EXIT_FAILURE); 

Príklad 1: Vytvorme program na použitie funkcie EXIT_FAILURE alebo 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); } 

Výkon

bfs a dfs
 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 

Príklad 2: Vytvorme ďalší program, ktorý použije EXIT_FAILURE na ukončenie programu 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; } 

Výkon

 Unable to open the defined file.