The funkcija exit(). se uporablja za takojšnjo prekinitev klica procesa ali funkcije v programu. To pomeni, da se vsaka odprta datoteka ali funkcija, ki pripada procesu, takoj zapre, ko se v programu pojavi funkcija exit(). Funkcija exit() je standardna funkcija knjižnice C, ki je definirana v stdlib.h naslovna datoteka. Torej lahko rečemo, da je funkcija, ki prisilno prekine trenutni program in prenese nadzor na operacijski sistem za izhod iz programa. Funkcija exit(0) ugotovi, da se program zaključi brez sporočila o napaki, nato pa funkcija exit(1) ugotovi, da program prisilno prekine postopek izvajanja.
Pomembne točke funkcije exit().
Sledijo glavne točke izhodne funkcije v programiranju C, kot sledi:
- Med uporabo funkcije exit () moramo vključiti datoteko glave stdlib.h.
- Uporablja se za prekinitev običajnega izvajanja programa, ko naleti na funkcijo exit ().
- Funkcija exit () pokliče registrirano funkcijo atexit() v obratnem vrstnem redu njihove registracije.
- Funkcijo exit() lahko uporabimo za izpiranje ali čiščenje vseh odprtih podatkov toka, kot je branje ali pisanje z nezapisanimi podatki v medpomnilniku.
- Zaprl je vse odprte datoteke, povezane z nadrejeno ali drugo funkcijo ali datoteko, in lahko odstrani vse datoteke, ustvarjene s funkcijo tmpfile.
- Vedenje programa je nedefinirano, če uporabnik večkrat pokliče funkcijo za izhod ali pokliče funkcijo za izhod in quick_exit.
- Funkcija izhoda je kategorizirana v dva dela: izhod (0) in izhod (1).
Sintaksa funkcije exit().
void exit ( int status);
The izhod() funkcija nima povratne vrste.
kaj pomeni xdxd
stanje int: Predstavlja statusno vrednost izhodne funkcije, vrnjeno nadrejenemu procesu.
Primer 1: Program za uporabo funkcije exit() v zanki for
jvm v Javi
Izdelajmo program za prikaz funkcije izhoda (0) za običajno prekinitev procesa v programskem jeziku C.
#include #include int main () { // declaration of the variables int i, num; printf ( ' Enter the last number: '); scanf ( ' %d', &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 '0' represents the successful termination of the program without any error, or programming failure occurs during the program'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's create a simple program to demonstrate the working of the exit(0) function in C programming.</p> <pre> #include #include int main () { printf ( ' Start the execution of the program. '); printf (' Exit from the program. '); // use exit (0) function to successfully execute the program exit (0); printf ( 'Terminate the execution of the program. '); 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'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(' Enter the character: '); scanf (' %c', &ch); // use if statement to check the condition if ( ch == 'Y') { printf(' Great, you did it. '); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (' You entered wrong character!! '); } 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 '1' 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's create a program to use the EXIT_FAILURE or exit(1) function</strong> </p> <pre> #include #include int main () { int num1, num2; printf (' Enter the num1: '); scanf ('%d', &num1); printf (' Enter the num2: '); scanf ('%d', &num2); if (num2 == 0) { printf (' Dividend cannot be zero. '); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (' %d / %d : %f', 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'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 ( 'javatpoint.txt', 'r' ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, 'Unable to open the defined file ' ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( ' Normal termination of the program. '); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Unable to open the defined file. </pre> <hr></num;>
V C obstajata dve vrsti statusa izhoda
Sledijo tipi izhodne funkcije v programskem jeziku C, kot sledi:
- IZHOD_ USPEH
- EXIT_FAILURE
IZHOD_USPEH : EXIT_SUCCESS je vrsta funkcije exit(), ki je predstavljena s stavkom exit(0). Kjer '0' predstavlja uspešno prekinitev programa brez kakršne koli napake ali pa se med izvajanjem programa pojavi napaka pri programiranju.
Sintaksa EXIT SUCCESS
exit (EXIT_SUCCESS);
Primer 1: Program za predstavitev uporabe funkcije EXIT_SUCCESS ali exit(0).
Ustvarimo preprost program za predstavitev delovanja funkcije exit(0) v programiranju C.
#include #include int main () { printf ( ' Start the execution of the program. '); printf (' Exit from the program. '); // use exit (0) function to successfully execute the program exit (0); printf ( 'Terminate the execution of the program. '); return 0; }
Izhod
Start the execution of the program. Exit from the program.
Primer 2: Program za uporabo makra EXIT_SUCCESS v funkciji exit().
Ustvarimo program C za preverjanje, ali je znak prisoten ali ne.
natasha dalal
#include #include int main () { // declaration of the character type variable char ch; printf(' Enter the character: '); scanf (' %c', &ch); // use if statement to check the condition if ( ch == 'Y') { printf(' Great, you did it. '); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (' You entered wrong character!! '); } return 0; }
Izhod
Enter the character: Y Great, you did it.
EXIT_FAILURE : EXIT_FAILURE je makro funkcije exit() za nenormalno izvajanje in prekinitev programa. EXIT_FAILURE je predstavljen tudi kot funkcija exit(1). Ali '1' predstavlja nenormalno prekine program in prenese nadzor na operacijski sistem.
vicky kaushal starost
Sintaksa EXIT_FAILURE
exit (EXIT_FAILURE);
Primer 1: Ustvarimo program za uporabo funkcije EXIT_FAILURE ali exit(1).
#include #include int main () { int num1, num2; printf (' Enter the num1: '); scanf ('%d', &num1); printf (' Enter the num2: '); scanf ('%d', &num2); if (num2 == 0) { printf (' Dividend cannot be zero. '); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (' %d / %d : %f', num1, num2, num3); // use the EXIT_SUCCESS exit(0); }
Izhod
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
Primer 2: Ustvarimo drug program, ki bo uporabil EXIT_FAILURE za prekinitev programa C.
#include #include int main () { // declare the data type of a file FILE *fptr = fopen ( 'javatpoint.txt', 'r' ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, 'Unable to open the defined file ' ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( ' Normal termination of the program. '); return 0; }
Izhod
Unable to open the defined file.