Exit Function in C

Introduction

In the C Programming Language, the exit function calls all functions registered with at exit and terminates the program. File buffers are flushed, streams are closed, and temporary files are deleted.

The C library function void exit(int status) terminates the calling process immediately. Any open file descriptors belonging to the process are closed and any children of the process are inherited

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.

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 < stdlib.h > header and is included in the C standard libraries. The <stdlib.h> 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.

Syntax

The syntax for the function in the C Language is:

void exit(int status);

Returns

The exit function does not return anything.

Required Header

In the C Language, the required header for the function is:

#include <stdlib.h>

Applies To

In the C language,we can use the exit function in the following versions:

  1. ANSI/ISO 9899-1990

Exit Function in C

There are two types of exit status in C/C++:

  1. Exit Success: We indicate exit success by exit(0) statement. It means the successful termination of the program, i.e. program can execute without any error or interrupt.
#include <file.h>
#include <stdio.h>

int main()
{
	FILE* file;

	// opening the file in read-only mode
	file = fopen("myFile.txt", "r");

	printf("File opening!");

	// EXIT_SUCCESS
	exit(0);
}
Note: Create a file called ‘myFile.txt’ and run the code in your local device to see the output.

2. Exit Failure: We indicate exit failure by exit(1). It means the abnormal termination of the program, i.e. some error or interrupt has occurred. We can use different integers other than 1 to indicate different types of errors.

#include <file.h>
#include <stdio.h>

int main()
{
	FILE* file;

	// open the file in read-only mode
	file = fopen("myFile.txt", "r");

	if (file == NULL) {
		printf("Error in opening file");

		// EXIT_FAILURE
		exit(1);
	}

	// EXIT_SUCCESS
	exit(0);
}

Let us now write a basic program to print certain lines in C:

#include <stdio.h>
#include <stdlib.h>

int main () {
   printf("Start of GeeksToCode....\n");
   
   printf("Exiting GeeksToCode....\n");
   exit(0);

   printf("End of the program....\n");

   return(0);
}

Output

Start of GeeksToCode....
Exiting GeeksToCode....

Now the question is that if we have an exit() function then why C11 standard introduce _Exit()? Actually, the exit() function performs some cleaning before termination of the program like connection termination, buffer flushes, etc. The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example, it does not execute functions registered with at exit.

Conclusion

In this post, we have discussed exit function in C.

  1. Getline function In C
  2. String reverse function In C
  3. Ceil Function In C

Leave a Comment

Your email address will not be published. Required fields are marked *