CKTIS / C
exit — función de terminación de C23
`exit(status)` de `<stdlib.h>` causa terminación normal y no retorna. Primero llama funciones `atexit` en orden inverso de registro; después vacía y cierra flujos, elimina archivos `tmpfile` y finalmente devuelve el estado al entorno anfitrión.
Versión: C23
Sintaxis
[[noreturn]] void exit(int status);exit(status) de <stdlib.h> causa terminación normal y no retorna. Primero llama funciones atexit en orden inverso de registro; después vacía y cierra flujos, elimina archivos tmpfile y finalmente devuelve el estado al entorno anfitrión.
El ejemplo registra first y second, escribe body en búfer y llama a exit(EXIT_SUCCESS). body second first muestra el orden inverso de handlers y el vaciado posterior de la salida estándar; no hay ruta tras exit.
EXIT_SUCCESS, cero y EXIT_FAILURE eligen categorías definidas, pero su representación para el anfitrión depende de la implementación. Un segundo exit, combinarlo con quick_exit o un longjmp que sale de un handler es UB. No se llaman handlers at_quick_exit; el registro tardío sigue la regla especial de C23.
Ejemplos
#include <stdio.h>
#include <stdlib.h>
static void first(void) { fputs("first\n", stdout); }
static void second(void) { fputs("second ", stdout); }
int main(void) {
if (atexit(first) != 0 || atexit(second) != 0) return EXIT_FAILURE;
fputs("body ", stdout);
exit(EXIT_SUCCESS);
}
Resultado
body second first
El ejemplo registra `first` y `second`, escribe `body ` en búfer y llama a `exit(EXIT_SUCCESS)`. `body second first` muestra el orden inverso de handlers y el vaciado posterior de la salida estándar; no hay ruta tras `exit`.