Difference between Exit and Return
Define Exit()
- At the point when a client needs to leave a program from this capability is utilized.
- A void return type capability calls all capabilities enrolled at the exit and ends the program.
- Document supports are flushed, streams are shut, and impermanent records are erased and subsequently memory is liberated.
Syntax:
void exit(int status);
The boundaries utilized are as per the following:
Value Description
EXIT_SUCCESS Successful End
0 Successful End
EXIT_FAILURE Unsuccessful End
Define Break()
- This capability is for the most part used to emerge from a circle at the moment.
- At the point when a break explanation is executed it moves the control to the explanations that follow the switch or circle.
Syntax:
break;
Plain Distinction Between both the capabilities:
break() exit()
It is a keyword
It is a pre-characterized capability.
It requires no header record as
it is pre-characterized in stdio.h
header document in C. It requires header document stdlib.h for C, as opposed to for C++.
- It is many times utilized exclusively inside the circle and switch case statement.
- It is much of the time utilized anyplace inside the program.
- It can't be utilized as a variable name as it is a held word in the C language. It is definitely not a saved word thus, it is much of the time utilized as a variable name.
- In a C program, beyond what one break explanation can be executed. In a C program, only one leave capability will be executed.
Program 1:
The following are a C program and a C++ Program exhibiting the utilization of break:
// C++ program to show the utilization
// of break explanation
#incorporate <iostream>
utilizing namespace sexually transmitted disease;
// Driver Code
int fundamental()
{
// Neighborhood variable definition
int a = 10;
// While circle execution
while (a < 20) {
cout<<"worth of a:"<< a<<endl;
a++;
// end the circle utilizing
// break explanation
in the event that (a > 15) {
break;
}
}
cout<<"The break proclamation executed"
" when the worth "
" became "<< a;
return 0;
}
Output:
worth of a:10
worth of a:11
worth of a:12
worth of a:13
worth of a:14
worth of a:15
- The break explanation executed when the worth became 16
Clarification:
- In the above code, break ends the while circle when the condition is fulfilled and the code after the while circle will be executed in the wake of breaking the circle.
Program 2:
- The following are C and C++ program exhibiting the utilization of exit():
// C++ program to exhibit the
// utilization of exit()
#incorporate <iostream>
utilizing namespace sexually transmitted disease;
// Driver Code
int primary()
{
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;
}
bring 0 back;
}
Output:
I = 1 I = 2
Clarification:
- In the above code, after the leave capability is executed, the program gets ended and no code after that gets executed.