The Exit Function In C

ImportanLogic Practices of the exit function

Following are the main points of the exit function in C programming as follows:

  • We must include the stdlib.h header file while using the exit function.
  • It is used to terminate the normal execution of the program while encountered the exit function.
  • The exit function calls the registered atexit function in the reverse order of their registration.
  • We can use the exit function to flush or clean all open stream data like read or write with unwritten buffered data.
  • It closed all opened files linked with a parent or another function or file and can remove all files created by the tmpfile function.
  • The program's behaviour is undefined if the user calls the exit function more than one time or calls the exit and quick_exit function.
  • The exit function is categorized into two parts: exit(0) and exit(1).

Syntax of the exit function

Example

void exit ( int status);

The exit function has no return type.

The variable status indicates the status value that is returned to the parent process by the exit function.

Example 1: Code Demonstrating the Application of the exit Function within a For Loop

Let's develop a program to showcase the use of the exit (0) function for gracefully terminating the process in the C programming language.

Example

#include <stdio.h>
#include <stdlib.h>
int main () 
{
// declaration of the variables
int i, num;
printf ( " Enter the last number: ");
scanf ( " %d", &num);
for ( i = 1; i<num; i++)
{
// use if statement to check the condition
if ( i == 6 )

/* use exit () statement with passing 0 argument to show termination of the program without any error message. */
exit(0);  

else

printf  (" \n Number is %d", i);
} 
return 0;
}

Output

Output

Enter the last number: 10

 Number is 1
 Number is 2
 Number is 3
 Number is 4
 Number is 5

There are two types of exit status in C

Following are the categories of the exit function in the C programming language:

  • EXIT_ SUCCESS
  • EXIT_FAILURE

EXITSUCCESS: The EXITSUCCESS constant signifies the type of the exit function, denoted by the exit(0) statement. In this context, the value '0' indicates the successful completion of the program without encountering any errors or failures in the execution process.

Syntax of the EXIT SUCCESS

Example

exit (EXIT_SUCCESS);

Example 1: Illustration depicting the utilization of the EXIT_SUCCESS constant or the exit(0) function

Let's develop a basic program to showcase how the exit(0) function operates in C programming.

Example

#include <stdio.h>
#include <stdlib.>
int main ()
{
printf ( " Start the execution of the program.  \n");
printf (" Exit from the program. \n ");
// use exit (0) function to successfully execute the program
exit (0);
printf ( "Terminate the execution of the program.\n ");
return 0;
}

Output

Output

Start the execution of the program.
Exit from the program.

Example 2: Code snippet demonstrating the utilization of the EXIT_SUCCESS constant in the exit system call.

Let's develop a C program to verify if a specific character is present or not within a given input.

Example

#include <stdio.h>
#include <stdlib.h>
int main () 
{
// declaration of the character type variable
char ch;
printf(" Enter the character: ");
scanf (" %c", &ch);

// use if statement to check the condition
if ( ch == 'Y')
{
	printf(" Great, you did it. ");
	exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program
}
else
{
	printf (" You entered wrong character!! ");
}
return 0;
}

Output

Output

Enter the character: Y
Great, you did it.

The ```

Start the execution of the program.

Exit from the program.

Example


Syntax of the EXIT_FAILURE

exit (EXIT_FAILURE);

Example


Example 1: We will now develop a program that utilizes the EXIT_FAILURE constant or the exit(1) function.

include <stdio.h>

include <stdlib.h>

int main

{

int num1, num2;

printf (" Enter the num1: ");

scanf ("%d", &num1);

printf (" \n Enter the num2: ");

scanf ("%d", &num2);

if (num2 == 0)

{

printf (" \n Dividend cannot be zero. ");

// use EXIT_FAILURE

exit(1);

}

float num3 = (float)num1 / (float)num2;

printf (" %d / %d : %f", num1, num2, num3);

// use the EXIT_SUCCESS

exit(0);

}

Example


Output

Enter the num1: 20

Enter the num2: 6

20 / 6 : 3.333333

2nd Run

Enter the num1: 20

Enter the num2: 6

Dividend cannot be zero

Example


Let's develop an additional script that employs the EXIT_FAILURE constant to end the C program.

include <stdio.h>

include <stdlib.h>

int main

{

// declare the data type of a file

FILE *fptr = fopen ( "logic practice.txt", "r" );

// use if statement to check whether the fptr is null or not.

if ( fptr == NULL)

{

fprintf ( stderr, "Unable to open the defined file \n" );

exit ( EXIT_FAILURE); // use exit function to check termination

}

// use fclose function to close the file pointer

fclose (fptr);

printf ( " Normal termination of the program. ");

return 0;

}

Example


Output

Unable to open the defined file.

Example


Input Required

This code uses input(). Please provide values below: