Skip to content
Related Articles

Related Articles

Difference between exit() and break in C/C++

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 11 Jun, 2022
Improve Article
Save Article

In this article, the topic is to understand the difference between exit() and break.

exit():

  • When a user wants to exit a program from this function is used.
  • It is a void return type function that calls all functions registered at the exit and terminates the program.
  • File buffers are flushed, streams are closed, and temporary files are deleted and hence memory is freed.

syntax: 

void exit(int status);

The parameters used are as follows:

ValueDescription
EXIT_SUCCESSSuccessful Termination
0Successful Termination
EXIT_FAILUREUnsuccessful Termination

break():

  • This function is generally used to come out of a loop at the instant.
  • When a break statement is executed it transfers the control to the statements that follow the switch or loop.

syntax:

break;

Tabular Difference Between both the functions:

break() exit()
It is a keywordIt is a pre-defined function.
It doesn’t require any header file as it is pre-defined in stdio.h header file in C.It requires header file stdlib.h only for C, not for C++.
It terminates the loop.It terminates the program.
It is often used only within the loop and switch case statement.It is often used anywhere within the program.
It cannot be used as a variable name as it is a reserved word in the C language.It is not a reserved word so, it is often used as a variable name.
In a C program, more than one break statement can be executed.In a C program, just one exit function will be executed.

Program 1:

Below are a C program and a C++ Program demonstrating the use of break:

C++




// C++ program to demonstrate the use
// of break statement
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Local variable definition
    int a = 10;
 
    // While loop execution
    while (a < 20) {
 
        cout <<"value of a:"<< a<< endl;
        a++;
 
        // terminate the loop using
        // break statement
        if (a > 15) {
            break;
        }
    }
 
    cout <<"The break statement executed"
           " when the value "
           " became "<< a;
 
    return 0;
}
 
//this code is contributed by shivanisinghss2110

C




// C program to demonstrate the use
// of break statement
#include <stdio.h>
 
// Driver Code
int main()
{
    // Local variable definition
    int a = 10;
 
    // While loop execution
    while (a < 20) {
 
        printf("value of a: %d\n", a);
        a++;
 
        // terminate the loop using
        // break statement
        if (a > 15) {
            break;
        }
    }
 
    printf("The break statement executed"
           " when the value "
           " became %d\n",
           a);
 
    return 0;
}

Output

value of a:10
value of a:11
value of a:12
value of a:13
value of a:14
value of a:15
The break statement executed when the value  became 16

Explanation: In the above code, break terminates the while loop when the condition is satisfied and the code after the while loop will be executed after breaking the loop.

Program 2:

Below are C and C++ program demonstrating the use of exit(): 

C++




// C++ program to demonstrate the
// use of exit()
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        cout <<"i = "<< i << "\n";
    }
 
     
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            cout <<"j = "<< j;
    }
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C




// C program to demonstrate the
// use of exit()
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main()
{
    for (int i = 1; i < 5; i++) {
        if (i == 3)
            exit(0);
        printf("i = %d \t", i);
    }
 
    printf("\n");
 
    for (int j = 9; j > 0; j--) {
        if (j == 5)
            printf("j = %d \t", j);
    }
 
    return 0;
}

Output: 

i = 1     i = 2

 

Explanation: In the above code, after the exit function is executed, the program gets terminated and no code after that gets executed.

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!