logo

Funcția exit() din C

The funcția exit(). este folosit pentru a termina un proces sau o funcție care apelează imediat în program. Înseamnă că orice fișier deschis sau funcție aparținând procesului este închis imediat când funcția exit() a apărut în program. Funcția exit() este funcția standard de bibliotecă a C, care este definită în stdlib.h fișier antet. Deci, putem spune că este funcția care termină forțat programul curent și transferă controlul către sistemul de operare pentru a ieși din program. Funcția exit(0) determină terminarea programului fără niciun mesaj de eroare, iar apoi funcția exit(1) determină că programul încheie forțat procesul de execuție.

Funcția exit() din C

Puncte importante ale funcției exit().

Următoarele sunt punctele principale ale funcției de ieșire în programarea C, după cum urmează:

  1. Trebuie să includem fișierul antet stdlib.h în timp ce folosim funcția exit ().
  2. Este folosit pentru a termina execuția normală a programului în timp ce se întâlnește funcția de ieșire ().
  3. Funcția exit () apelează funcția atexit() înregistrată în ordinea inversă înregistrării lor.
  4. Putem folosi funcția exit() pentru a șterge sau curăța toate datele fluxului deschis, cum ar fi citirea sau scrierea cu date tampon nescrise.
  5. A închis toate fișierele deschise legate de un părinte sau de altă funcție sau fișier și poate elimina toate fișierele create de funcția tmpfile.
  6. Comportamentul programului este nedefinit dacă utilizatorul apelează funcția exit de mai multe ori sau apelează funcția exit și quick_exit.
  7. Funcția de ieșire este clasificată în două părți: exit(0) și exit(1).

Sintaxa funcției exit().

 void exit ( int status); 

The Ieșire() funcția nu are un tip de returnare.

comenzi rapide linux

stare int: Reprezintă valoarea de stare a funcției de ieșire returnată procesului părinte.

Exemplul 1: Program pentru a utiliza funcția exit() în bucla for

Să creăm un program pentru a demonstra funcția de ieșire (0) pentru terminarea normală a procesului în limbajul de programare 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;>

Există două tipuri de statut de ieșire în C

Următoarele sunt tipurile de funcție de ieșire în limbajul de programare C, după cum urmează:

  1. EXIT_ SUCCES
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_ SUCCESS este tipul de funcție exit(), care este reprezentat de instrucțiunea exit(0). Unde „0” reprezintă terminarea cu succes a programului fără nicio eroare, sau eșecul de programare are loc în timpul execuției programului.

comentariu css

Sintaxa EXIT SUCCESS

 exit (EXIT_SUCCESS); 

Exemplul 1: Program pentru a demonstra utilizarea funcției EXIT_SUCCESS sau exit(0)

Să creăm un program simplu pentru a demonstra funcționarea funcției exit(0) în programarea C.

10 ml în uncii
 #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; } 

Ieșire

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

Exemplul 2: Program pentru a utiliza macrocomanda EXIT_SUCCESS în funcția exit().

Să creăm un program C pentru a valida dacă personajul este prezent sau nu.

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

Ieșire

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE este macro-ul funcției exit() pentru a executa și a termina programul în mod anormal. EXIT_FAILURE este reprezentată și ca funcție exit(1). Dacă „1” reprezintă termină anormal programul și transferă controlul către sistemul de operare.

Sintaxa EXIT_FAILURE

 exit (EXIT_FAILURE); 

Exemplul 1: Să creăm un program care să utilizeze funcția EXIT_FAILURE sau 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); } 

Ieșire

ceva rapid
 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 

Exemplul 2: Să creăm un alt program pentru a folosi EXIT_FAILURE pentru a termina programul 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; } 

Ieșire

 Unable to open the defined file.