logo

Функцията exit() в C

The функция exit(). се използва за незабавно прекратяване на извикване на процес или функция в програмата. Това означава, че всеки отворен файл или функция, принадлежаща на процеса, се затваря незабавно, когато функцията exit() се появи в програмата. Функцията exit() е стандартната библиотечна функция на C, която е дефинирана в stdlib.h заглавен файл. Така че можем да кажем, че това е функцията, която принудително прекратява текущата програма и прехвърля контрола към операционната система за излизане от програмата. Функцията exit(0) определя, че програмата прекратява без никакво съобщение за грешка, а след това функцията exit(1) определя, че програмата принудително прекратява процеса на изпълнение.

Функцията exit() в C

Важни моменти от функцията exit().

Следват основните точки на функцията за изход в програмирането на C, както следва:

  1. Трябва да включим заглавния файл stdlib.h, докато използваме функцията exit ().
  2. Използва се за прекратяване на нормалното изпълнение на програмата, докато се среща функцията exit ().
  3. Функцията exit () извиква регистрираната функция atexit() в обратния ред на тяхната регистрация.
  4. Можем да използваме функцията exit(), за да изчистим или изчистим всички отворени данни от потока, като четене или запис с незаписани буферирани данни.
  5. Той затвори всички отворени файлове, свързани с родителска или друга функция или файл, и може да премахне всички файлове, създадени от функцията tmpfile.
  6. Поведението на програмата е недефинирано, ако потребителят извика функцията за изход повече от един път или извика функцията за изход и quick_exit.
  7. Функцията за изход е категоризирана в две части: изход (0) и изход (1).

Синтаксис на функцията exit().

 void exit ( int status); 

The изход() функцията няма тип връщане.

произволен ред sql

int състояние: Той представлява стойността на състоянието на изходната функция, върната към родителския процес.

Пример 1: Програма за използване на функцията exit() в for цикъла

Нека създадем програма, за да демонстрираме функцията exit (0) за нормално прекратяване на процеса на езика за програмиране C.

какво е 10 от 60
 #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

Следват типовете функция за изход в езика за програмиране C, както следва:

  1. ИЗХОД_ УСПЕХ
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_SUCCESS е тип функция exit(), която е представена от командата exit(0). Където '0' представлява успешното прекратяване на програмата без грешка или грешка в програмирането възниква по време на изпълнение на програмата.

Синтаксис на EXIT SUCCESS

 exit (EXIT_SUCCESS); 

Пример 1: Програма за демонстриране на използването на функцията EXIT_SUCCESS или exit(0).

Нека създадем проста програма, за да демонстрираме работата на функцията exit(0) в C програмирането.

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

Изход

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

Пример 2: Програма за използване на макроса EXIT_SUCCESS във функцията exit().

10 ml до унции

Нека създадем програма на C, за да проверим дали символът присъства или не.

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

Изход

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE е макросът на функцията exit() за необичайно изпълнение и прекратяване на програмата. EXIT_FAILURE също се представя като функцията exit(1). Дали „1“ представлява необичайно прекратяване на програмата и прехвърляне на контрола към операционната система.

Синтаксис на EXIT_FAILURE

java докато условие
 exit (EXIT_FAILURE); 

Пример 1: Нека създадем програма, която да използва функцията EXIT_FAILURE или 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); } 

Изход

 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: Нека създадем друга програма, която да използва EXIT_FAILURE за прекратяване на 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; } 

Изход

 Unable to open the defined file.