Javatpoint Logo
Javatpoint Logo

Abort() Function in C Programming

The abort() function is a function in the C library that bring you out from the program from where this function is called in the program.

It initiates a SIGABRT signal. The signal leads to the termination of the program that is being currently executed. It relieves all the buffers and ensures that all the files are closed.

The program may even have a handler. The responsibility of the handler includes intercepting the SIGABRT signal. It returns the control to the host environment. If the handler does not catch the signal, then an abnormal shutdown of the program occurs.

Note: When the program's termination is not normal, the user will be unable to use atexit() functions.

The user can also use a handler using the abort() function. The handler is used to handle the SIGABRT signal after it is initiated. The signal is generated again when the programs return the handler for any default action is returned. The code comes across the handle only once, even when resetting the handler.

The same is the case with the signal, once the handler ignores it. It will lead to the termination of the program.

The program's termination can be stopped even after executing the abort() function if the SIGABRT signal is caught by the handler or if it is ignored. You can use several methods in your program to avoid a return from the handler. One is you can get directly out of the handler.

Use the following functions to jump out of the handler:

  • setjmp()
  • longjmp()

Declaring the abort() function

The syntax of declaring abort() is similar to any other function. It begins with the return type, then followed by the function name that is abort(). And the parameter inside the brackets.

Thus, the function does not require any parameter and also it does not return any

value. So for both, we use the datatype void.

Implementing abort() in C:

Let us discuss an example to implement the abort() function:

Output:

Abort() Function in C Programming

Given above is the output after the compilation and execution of the program. The result of the program will be as follows:

The program will try opening the thisfile.txt file, but no file exists with this name. So, when the code tries to open the file, it fails, and the code is aborted.

The steps are as follows:

  • Open the thisfile.txt
  • Abort the program
  • Dump the core.

Difference between abort() and exit()

The exit() function ensures that the program is terminated properly. It closes all the files that were opened during the execution of the function. It clears the buffer and calls the functions listed in atexit().

When using the abort(), it initiates a SIGABRT signal that terminates the program if not caught by the handler in the program. But if the handler intercepts the signal, it may prevent the termination. The termination is unusual in this case. It may not close the file or not erase the temporary files that were created during the execution.

On using exit(), the functions that are registered with atexit() cannot be implemented. If the user wants to clear the stream buffer, it is better to use the exit() function, or the programmer should include a handler for SIGABRT in the code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA