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.


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

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


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

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started