Exit() is a core function in the C/C programming language that is used to instantly end the calling process (function). It is possible to call from any function. It informs the operating system of the state of program termination by passing an int value. It is usually used when software crashes unexpectedly. It is also recognized as the program’s current state. The exit function can be used to end a program at any moment, regardless of how many function calls have been performed. It calls a variety of other functions before terminating the application, such as shutting open files, to clean up.

The exit function is specified in the header and is included in the C standard libraries. The file defines several types, macros, and basic utility functions for performing numerical conversions, random number generation, sorting, memory management, and interacting with the environment. To interrelate with the environment, utilize the exit function.

Parameters

Parameters of exit() function in the C programming language are described underneath.

EXIT_SUCCESS

If the passed status argument is zero or the int valued macro: EXIT_SUCCESS, described in the header, a successful status should be passed to the host environment, and thus an execution defined form of the successful status is brought back to the hosting environment, toward whom control is conceded. Exit(0) is used to generally terminate a program by indicating that the operation was accomplished. EXIT_SUCCESS has a value of 0.

As a result, rather than exit, we could use exit(EXIT_SUCCESS). EXIT_SUCCESS is a prefix that extends into integer expressions that can be passed to the method exit as an argument. And exit(0) signifies a clean exit from the program with no errors.

EXIT_FAILURE

If the supplied status argument is the int-valued macro EXIT_FAILURE, specified in the header, the exit function will return an execution version of the failure result to the hosting environment, to which control is specified. It can also be used to end the program normally but with the condition that the operation failed. EXIT_FAILURE has a value of 1.

As a result, rather than exit, we can use exit EXIT_FAILURE. EXIT_FAILURE is a phrase that can be used for a range of purposes. Exit status 1 represents that there was a runtime issue, which could have been caused by a programming error.

Example 1

We have an example to illustrate the basic functionality of the exit() function in the C programming language. Make a file in your GCC compiler that is configured in your Windows 10 system. The file name can be dependent on your choice but its extension must be “.c”. At the start of our program, few libraries have been added for the smooth execution of code. After that, we have the main function. Initially, we have used two printf() statements with “n” specifier that will print their result in two separate lines.

After these two statements, exit(0) has been called. The functionality of exit(0) has been explained in detail in the parameters segment of this guide. After this function call, another print statement has been used but it will not be displayed on the output screen. The reason for this implementation is that exit(0) has been used before the third statement. Now, save and close the file to get the result of your program.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/exit-function-c-01.png" data-lazy- height="486" src="data:image/svg xml,” width=”936″>

As soon as you save the file, you have to compile and run your example code, as rapidly as you hit the “Run and Compile” option of your GCC compiler, the console will pop up on your screen which exhibits the expected result.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/exit-function-c-02.png" data-lazy- height="260" src="data:image/svg xml,” width=”936″>

Example 2

Now, we are moving towards our second example to demonstrate the simple functionality of the exit() function in the C programming language. Again, build a file in your GCC compiler that is configured in your Windows 10 system or utilize the former one by clearing the previous code. The file name can be reliant on your choice but its extension must be “.c”. At the start of our program, few libraries have been introduced for the smooth implementation of code. After that, we have the main function. Initially, we have used one printf() statement.

After this statement, exit(0) has been called. It will just terminate the program here without moving further. The functionality of exit(0) has been clarified in detail in the parameters section of this guide. After this function call, another print statement has been utilized but it will not be displayed on the output screen. The reason for this implementation is that exit(0) has been used before the second statement. Now, save and close the file to get the output of your program.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/exit-function-c-03.png" data-lazy- height="388" src="data:image/svg xml,” width=”936″>

As soon as you save the file, you have to compile and run your sample code, as quickly as you hit the “Run and Compile” option of your GCC compiler, the console will pop up on your screen which shows the predictable result.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/exit-function-c-04.png" data-lazy- height="230" src="data:image/svg xml,” width=”936″>

Conclusion

This article is all about exit() function in the C programming language. We have discussed its concept and parameters in detail so that you will have a grip on the examples that we have explained in this guide. You can amend these examples of the exit() function to check its functionality in the C programming language.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content