C - Terminate main function

Introduction

The exit() function is used to gracefully quit a program.

In the following code the exit() function is used leave the program in the sub() function.

Demo

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

void sub(void); 

int main() /*from w ww .j  a  va2 s  .  co  m*/
{ 
    puts("This program quits before it's done."); 
    sub(); 
    puts("Or was that on purpose?"); 
    return(0); 
} 

void sub(void) 
{ 
    puts("Which is the plan."); 
    exit(0); 
}

Result

You need to include the stdlib.h header file to use the exit() function.

It uses an int value as an argument for the exit status, similar to the value passed by return in the main() function.

Related Topic