logo

La función exit() en C

El función de salida () se utiliza para finalizar una llamada de proceso o función inmediatamente en el programa. Significa que cualquier archivo abierto o función que pertenezca al proceso se cierra inmediatamente cuando se produjo la función exit() en el programa. La función exit() es la función de biblioteca estándar de C, que se define en el stdlib.h archivo de cabecera. Entonces, podemos decir que es la función que finaliza forzosamente el programa actual y transfiere el control al sistema operativo para salir del programa. La función exit(0) determina que el programa termina sin ningún mensaje de error, y luego la función exit(1) determina que el programa termina forzosamente el proceso de ejecución.

La función exit() en C

Puntos importantes de la función exit()

Los siguientes son los puntos principales de la función de salida en la programación en C:

  1. Debemos incluir el archivo de encabezado stdlib.h mientras usamos la función exit ().
  2. Se utiliza para finalizar la ejecución normal del programa mientras se encuentra la función exit ().
  3. La función exit () llama a la función atexit() registrada en el orden inverso a su registro.
  4. Podemos usar la función exit() para vaciar o limpiar todos los datos de flujo abierto, como lectura o escritura, con datos almacenados en búfer no escritos.
  5. Cerró todos los archivos abiertos vinculados con una función o archivo principal u otra y puede eliminar todos los archivos creados por la función tmpfile.
  6. El comportamiento del programa no está definido si el usuario llama a la función de salida más de una vez o llama a la función de salida y salida rápida.
  7. La función de salida se clasifica en dos partes: salida (0) y salida (1).

Sintaxis de la función exit()

 void exit ( int status); 

El salida() La función no tiene tipo de retorno.

ordenar burbujas en java

estado entero: Representa el valor de estado de la función de salida devuelta al proceso principal.

Ejemplo 1: Programa para usar la función exit() en el bucle for

cadena a carácter

Creemos un programa para demostrar la función de salida (0) para finalizar normalmente el proceso en el lenguaje de programación C.

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

Hay dos tipos de estado de salida en C

Los siguientes son los tipos de función de salida en el lenguaje de programación C, como sigue:

  1. SALIR_ ÉXITO
  2. EXIT_FAILURE

SALIR_ÉXITO : EXIT_SUCCESS es el tipo de función exit(), que está representada por la instrucción exit(0). Donde el '0' representa la finalización exitosa del programa sin ningún error, o se produce una falla de programación durante la ejecución del programa.

Sintaxis de SALIDA ÉXITO

 exit (EXIT_SUCCESS); 

Ejemplo 1: Programa para demostrar el uso de la función EXIT_SUCCESS o exit(0)

Creemos un programa simple para demostrar el funcionamiento de la función exit(0) en programación 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; } 

Producción

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

Ejemplo 2: Programa para utilizar la macro EXIT_SUCCESS en la función exit()

Creemos un programa en C para validar si el personaje está presente o no.

lista de religiones
 #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; } 

Producción

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE es la macro de la función exit() para ejecutar y finalizar de forma anormal el programa. EXIT_FAILURE también se representa como la función exit(1). Si el '1' representa la terminación anormal del programa y transfiere el control al sistema operativo.

10 1 millón

Sintaxis de EXIT_FAILURE

 exit (EXIT_FAILURE); 

Ejemplo 1: creemos un programa para usar la función EXIT_FAILURE o 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); } 

Producción

 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 

Ejemplo 2: creemos otro programa para usar EXIT_FAILURE para finalizar el programa 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; } 

Producción

 Unable to open the defined file.