What is exit() function in C language?


The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system.

The general form of the exit() function is as follows −

void exit (int code);

The value of the code is returned to the calling process, which is done by an operation system. Generally, zero is used as return code to indicate normal program termination.

Example

Following is the C program for use of exit() function

#include<stdio.h>
void main(){
   char ch;
   printf("B: Breakfast");
   printf("L: Lunch");
   printf("D: Dinner");
   printf("E: Exit");
   printf("Enter your choice:");
   do{
      ch = getchar();
      switch (ch){
         case 'B' :
         printf ("time for breakfast");
         break;
         case 'L' :
         printf ("time for lunch");
         break;
         case 'D' :
         printf ("time for dinner");
         break;
         case 'E' :
         exit(0); /* return to operating system */
      }
   } while (ch != 'B' && ch != 'L' && ch != 'D');
   return 0;
}

Output

When the above program is executed, it produces the following result −

B: Breakfast
L: Lunch
D: Dinner
E: Exit
Enter your choice:D
Time for dinner

Updated on: 01-Sep-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements