Table of contents
1.
Introduction
2.
exit()
3.
_Exit()
4.
Differences between exit() and _Exit()
5.
 
6.
FAQs
6.1.
What is exit status?
6.2.
What is atexit?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

exit() vs _Exit()

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online C Compiler
Run Code

 

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();
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Main()
func1

_Exit()

The _Exit function performs minimal C library termination procedures, terminates the process or program execution, and provides the exit status code to the calling process. Unlike the exit() function, it does not perform any cleanup tasks that involve clearing used resources such as files or data.

 

Prototype:

void _Exit(int exit_status);
You can also try this code with Online C Compiler
Run Code

 

#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();   
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Main()
func1

 

You can also read about the jump statement, Tribonacci Series

Differences between exit() and _Exit()

exit()

_Exit()

It performs cleanup tasks and then terminates the program. It does not perform any cleanup tasks before terminating the program.
It can execute functions registered with atexit. It does not execute functions registered with atexit.

 

 

 



Also see,  Short int in C Programming

FAQs

What is exit status?

If the status value is 0 or EXIT_SUCCESS, it indicates successful termination is returned to the host environment. If the status value is EXIT_FAILURE, it indicates unsuccessful termination and is returned.

What is atexit?

The atexit function causes the specified function to be called when the program terminates. It will be called at the time of program termination irrespective of where the terminating function is registered. It returns zero if the registration succeeds and nonzero if it fails.

Conclusion

This article extensively discusses the exit() and _Exit() functions in C. We hope that this blog has helped you enhance your knowledge of the working and differences between the two exit functions. If you would like to learn more, check out our articles on  Basics of C and Functions in C. Learn more about ArraysPointersMemory managementStructures and Unions in C.

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc? Have a look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them helpful and engaging! Happy Coding!

Live masterclass