logo

Funkcija iziet () valodā C

The exit() funkcija tiek izmantots, lai programmā nekavējoties pārtrauktu procesa vai funkcijas izsaukšanu. Tas nozīmē, ka jebkurš atvērtais fails vai funkcija, kas pieder procesam, tiek nekavējoties aizvērta, kad programmā notika funkcija exit (). Funkcija exit() ir C standarta bibliotēkas funkcija, kas ir definēta stdlib.h galvenes fails. Tātad, mēs varam teikt, ka tā ir funkcija, kas piespiedu kārtā pārtrauc pašreizējo programmu un nodod vadību operētājsistēmai, lai izietu no programmas. Funkcija exit (0) nosaka, ka programma tiek pārtraukta bez kļūdas ziņojuma, un pēc tam funkcija exit (1) nosaka, ka programma piespiedu kārtā pārtrauc izpildes procesu.

Funkcija iziet () valodā C

Svarīgi izejas() funkcijas punkti

Tālāk ir norādīti C programmēšanas izejas funkcijas galvenie punkti:

  1. Izmantojot izejas () funkciju, mums ir jāiekļauj galvenes fails stdlib.h.
  2. To izmanto, lai pārtrauktu normālu programmas izpildi, kamēr tiek konstatēta izejas () funkcija.
  3. Funkcija Exit () izsauc reģistrēto atexit() funkciju to reģistrācijas apgrieztā secībā.
  4. Mēs varam izmantot funkciju exit(), lai izskalotu vai notīrītu visus atvērtos straumes datus, piemēram, lasītu vai rakstītu ar nerakstītiem buferizētiem datiem.
  5. Tas aizvēra visus atvērtos failus, kas saistīti ar vecāku vai citu funkciju vai failu, un var noņemt visus failus, kas izveidoti, izmantojot funkciju tmpfile.
  6. Programmas darbība nav definēta, ja lietotājs izsauc izejas funkciju vairāk nekā vienu reizi vai izsauc funkcijas exit un quick_exit.
  7. Izejas funkcija ir iedalīta divās daļās: izeja (0) un izeja (1).

Funkcijas exit() sintakse

 void exit ( int status); 

The Izeja() funkcijai nav atgriešanas veida.

vārds

int statuss: Tas atspoguļo vecākprocesam atgrieztās izejas funkcijas statusa vērtību.

1. piemērs: programma, lai izmantotu funkciju exit() for cilpā

java stīgu apdare

Izveidosim programmu, lai demonstrētu izejas (0) funkciju normālai procesa pārtraukšanai C programmēšanas valodā.

 #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 ir divu veidu izejas statuss

Tālāk ir norādīti C programmēšanas valodas izejas funkcijas veidi:

  1. EXIT_ SUCCESS
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_ SUCCESS ir funkcijas exit() tips, ko attēlo exit(0) priekšraksts. Kur “0” apzīmē veiksmīgu programmas izbeigšanu bez kļūdām vai programmas izpildes laikā rodas programmēšanas kļūme.

IZEŠANAS VEIKSMES sintakse

 exit (EXIT_SUCCESS); 

1. piemērs: Programma, kas demonstrē funkcijas EXIT_SUCCESS vai izejas(0) izmantošanu

Izveidosim vienkāršu programmu, lai demonstrētu funkcijas exit(0) darbību C programmēšanā.

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

Izvade

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

2. piemērs. Programma makro EXIT_SUCCESS izmantošanai funkcijā exit()

Izveidosim C programmu, lai pārbaudītu, vai varonis ir vai nav.

r in c programmēšana
 #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; } 

Izvade

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE ir funkcijas exit() makro, lai neparasti izpildītu un pārtrauktu programmu. EXIT_FAILURE tiek attēlota arī kā izejas (1) funkcija. Neatkarīgi no tā, vai '1' apzīmē, tiek pārtraukta programmas darbība un vadība tiek nodota operētājsistēmai.

virkne sadalīta java

EXIT_FAILURE sintakse

 exit (EXIT_FAILURE); 

1. piemērs: izveidosim programmu, lai izmantotu funkciju EXIT_FAILURE vai 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); } 

Izvade

 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. piemērs. Izveidosim citu programmu, lai izmantotu EXIT_FAILURE, lai pārtrauktu C programmu.

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

Izvade

 Unable to open the defined file.