logo

C의 Exit() 함수

그만큼 종료() 함수 프로그램에서 즉시 호출되는 프로세스나 함수를 종료하는 데 사용됩니다. 이는 프로그램에서 exit() 함수가 발생하면 열려 있는 파일이나 프로세스에 속하는 함수가 즉시 닫히는 것을 의미합니다. Exit() 함수는 C의 표준 라이브러리 함수로, stdlib.h 헤더 파일. 그러니까 현재 프로그램을 강제 종료하고 운영체제에 제어권을 넘겨 프로그램을 종료시키는 기능이라고 할 수 있습니다. Exit(0) 함수는 프로그램이 오류 메시지 없이 종료되었다고 판단하고, Exit(1) 함수는 프로그램이 실행 프로세스를 강제 종료한다고 판단합니다.

C의 Exit() 함수

Exit() 함수의 중요 포인트

C 프로그래밍에서 종료 함수의 주요 내용은 다음과 같습니다.

  1. Exit() 함수를 사용하는 동안 stdlib.h 헤더 파일을 포함해야 합니다.
  2. Exit() 함수를 만났을 때 프로그램의 정상적인 실행을 종료하는데 사용됩니다.
  3. Exit() 함수는 등록된 atexit() 함수를 등록의 역순으로 호출합니다.
  4. exit() 함수를 사용하여 기록되지 않은 버퍼링된 데이터로 읽기 또는 쓰기와 같은 모든 오픈 스트림 데이터를 플러시하거나 정리할 수 있습니다.
  5. 상위 또는 다른 함수나 파일과 연결된 열려 있는 모든 파일을 닫았으며 tmpfile 함수로 생성된 모든 파일을 제거할 수 있습니다.
  6. 사용자가 종료 함수를 두 번 이상 호출하거나 종료 및 Quick_exit 함수를 호출하는 경우 프로그램의 동작은 정의되지 않습니다.
  7. 종료 함수는 종료(0)와 종료(1)의 두 부분으로 분류됩니다.

exit() 함수의 구문

 void exit ( int status); 

그만큼 출구() 함수에는 반환 유형이 없습니다.

공장 디자인 패턴

정수 상태: 상위 프로세스로 반환된 종료 함수의 상태 값을 나타냅니다.

예제 1: for 루프에서 exit() 함수를 사용하는 프로그램

C 프로그래밍 언어로 프로세스를 정상적으로 종료하기 위한 종료(0) 기능을 보여주는 프로그램을 만들어 보겠습니다.

 #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_SUCCESS는 Exit() 함수 유형으로, Exit(0) 문으로 표현됩니다. 여기서 '0'은 오류 없이 프로그램이 성공적으로 종료되었거나 프로그램 실행 중에 프로그래밍 오류가 발생했음을 나타냅니다.

EXIT SUCCESS 구문

 exit (EXIT_SUCCESS); 

예제 1: EXIT_SUCCESS 또는 exit(0) 함수의 사용법을 보여주는 프로그램

C 프로그래밍에서 exit(0) 함수의 작동을 보여주는 간단한 프로그램을 만들어 보겠습니다.

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

산출

Java 객체를 json으로
 Start the execution of the program. Exit from the program. 

예제 2: exit() 함수에서 EXIT_SUCCESS 매크로를 사용하는 프로그램

캐릭터가 존재하는지 여부를 확인하는 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() 함수의 매크로이다. EXIT_FAILURE는 exit(1) 함수로도 표현됩니다. '1'은 프로그램이 비정상적으로 종료되고 운영 체제로 제어권이 넘어간 것을 의미하는지 여부.

EXIT_FAILURE 구문

 exit (EXIT_FAILURE); 

예제 1: EXIT_FAILURE 또는 exit(1) 함수를 사용하는 프로그램을 만들어 보겠습니다.

sed 명령
 #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.