Introduction
Calling functions in a program is a common practice to write good code. Sometimes for various reasons, we may have to abruptly terminate the calling function immediately without executing further processes. There are different ways to terminate a program in C.
-
exit()
-
_Exit()
-
abort
-
quick_exit
-
at_quick_exit
In this blog, we shall mainly discuss the exit() and _Exit() functions.
Also Read About, Sum of Digits in C, C Static Function
exit()
This function is part of the stdlib.h header file. The exit() function is entirely different from a flow control statement. It does not affect the control flow; instead, it exits the program under execution completely. It forcefully terminates the program execution and returns the control back to the operating system.
Prototype:
void exit(int return_value)
The return_value is returned to the calling process, hence the operating system. 0 indicates normal execution, while other values indicate an error.
Two macros used as arguments here are EXIT_SUCCESS and EXIT_FAILURE to show the successful and failed execution status of the program.
A highlight of using the exit() function is that it cleans up all the resources used by the program and then proceeds to terminate it.
#include <stdlib.h>
#include <stdio.h>
void f1(void)
{
puts("func1");
}
void f2(void)
{
puts("func2");
}
int main(void)
{
printf("Main()\n");
f1();
exit(0);
f2();
}
Output:
Main()
func1