logo

Die Funktion „exit()“ in C

Der Funktion „exit()“. wird verwendet, um einen Prozess- oder Funktionsaufruf im Programm sofort zu beenden. Dies bedeutet, dass alle geöffneten Dateien oder Funktionen, die zum Prozess gehören, sofort geschlossen werden, wenn die Funktion „exit()“ im Programm ausgeführt wird. Die Funktion „exit()“ ist die Standardbibliotheksfunktion von C, die in definiert ist stdlib.h Header-Datei. Wir können also sagen, dass es sich um die Funktion handelt, die das aktuelle Programm zwangsweise beendet und die Kontrolle an das Betriebssystem übergibt, um das Programm zu beenden. Die Funktion „exit(0)“ bestimmt, dass das Programm ohne Fehlermeldung beendet wird, und dann stellt die Funktion „exit(1)“ fest, dass das Programm den Ausführungsprozess zwangsweise beendet.

Die Funktion „exit()“ in C

Wichtige Punkte der Funktion exit()

Im Folgenden sind die Hauptpunkte der Exit-Funktion in der C-Programmierung aufgeführt:

  1. Wir müssen die Header-Datei stdlib.h einbinden, während wir die Funktion „exit()“ verwenden.
  2. Es wird verwendet, um die normale Ausführung des Programms zu beenden, während die Funktion „exit()“ aufgerufen wird.
  3. Die Funktion „exit()“ ruft die registrierte Funktion „atexit()“ in der umgekehrten Reihenfolge ihrer Registrierung auf.
  4. Wir können die Funktion „exit()“ verwenden, um alle offenen Stream-Daten wie Lese- oder Schreibvorgänge mit ungeschriebenen gepufferten Daten zu leeren oder zu bereinigen.
  5. Es schließt alle geöffneten Dateien, die mit einer übergeordneten oder einer anderen Funktion oder Datei verknüpft sind, und kann alle von der tmpfile-Funktion erstellten Dateien entfernen.
  6. Das Verhalten des Programms ist undefiniert, wenn der Benutzer die Exit-Funktion mehr als einmal aufruft oder die Exit- und Quick_exit-Funktion aufruft.
  7. Die Exit-Funktion ist in zwei Teile kategorisiert: Exit(0) und Exit(1).

Syntax der Funktion „exit()“.

 void exit ( int status); 

Der Ausfahrt() Funktion hat keinen Rückgabetyp.

Android-Telefon-Einstellungsmenü

int-Status: Es stellt den Statuswert der Exit-Funktion dar, der an den übergeordneten Prozess zurückgegeben wird.

Beispiel 1: Programm zur Verwendung der Funktion „exit()“ in der for-Schleife

Lassen Sie uns ein Programm erstellen, um die Exit-Funktion (0) zum normalen Beenden des Prozesses in der Programmiersprache C zu demonstrieren.

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

In C gibt es zwei Arten von Exit-Status

Im Folgenden sind die Typen der Exit-Funktion in der Programmiersprache C aufgeführt:

  1. EXIT_ ERFOLGREICH
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_ SUCCESS ist der Funktionstyp „exit()“, der durch die Anweisung „exit(0)“ dargestellt wird. Dabei steht „0“ für die erfolgreiche Beendigung des Programms ohne Fehler oder für einen Programmierfehler während der Ausführung des Programms.

Syntax des EXIT SUCCESS

Heap und Heap-Sortierung
 exit (EXIT_SUCCESS); 

Beispiel 1: Programm zur Demonstration der Verwendung der Funktion EXIT_SUCCESS oder exit(0).

Java-Verbindung MySQL

Lassen Sie uns ein einfaches Programm erstellen, um die Funktionsweise der Funktion „exit(0)“ in der C-Programmierung zu demonstrieren.

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

Ausgabe

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

Beispiel 2: Programm zur Verwendung des EXIT_SUCCESS-Makros in der Funktion „exit()“.

Erstellen wir ein C-Programm, um zu überprüfen, ob das Zeichen vorhanden ist oder nicht.

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

Ausgabe

Bash-Variable
 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE ist das Makro der Funktion „exit()“, um das Programm abnormal auszuführen und zu beenden. EXIT_FAILURE wird auch als Funktion „exit(1)“ dargestellt. Ob die „1“ bedeutet, dass das Programm abnormal beendet wird und die Steuerung an das Betriebssystem übergeben wird.

Syntax von EXIT_FAILURE

 exit (EXIT_FAILURE); 

Beispiel 1: Erstellen wir ein Programm zur Verwendung der Funktion EXIT_FAILURE oder 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); } 

Ausgabe

 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 

Beispiel 2: Erstellen wir ein weiteres Programm, um EXIT_FAILURE zum Beenden des C-Programms zu verwenden.

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

Ausgabe

 Unable to open the defined file.