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

02 Dec, 2021
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:
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;
Program 1:
Below is a C program demonstrating the use of break:
C++
C
// C++ program to demonstrate the use// of break statement#include <iostream>using namespace std; // Driver Codeint 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 when the value "           " became "<< a;     return 0;} //this code is contributed by shivanisinghss2110
// C program to demonstrate the use// of break statement#include <stdio.h> // Driver Codeint 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 when the value "           " became %d\n",           a);     return 0;}
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 when the value  became 16
 
Explanation: In the above code, break terminates the while loop when the if the condition is satisfied and the code hereafter while loop will be executed after breaking the loop.
Program 2:
Below is C program demonstrating the  use of exit(): 
C++
C
// C++ program to demonstrate the// use of exit()#include <iostream>using namespace std; // Driver Codeint 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 program to demonstrate the// use of exit()#include <stdio.h>#include <stdlib.h> // Driver Codeint 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;}
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.
 
simmytarika5
shivanisinghss2110
saimthis1997
C Basics
exit
C Language
C Programs
Difference Between
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Multithreading in C
Exception Handling in C++
'this' pointer in C++
Arrow operator -> in C/C++ with Examples
Strings in C
Arrow operator -> in C/C++ with Examples
UDP Server-Client implementation in C
C Program to read contents of Whole File
Header files in C/C++ and its uses