The major difference between break
and exit()
is that break
is a keyword, which causes an immediate exit from the switch
or loop (for
, while
or do
), while exit()
is a standard library function, which terminates program execution when it is called.
The general syntax of the exit()
function is
void exit(int return_code);
return_code
is returned to the calling process, so the success or failure of the program can be tested by another program that uses this one as a sub-process. Conventionally, a return value of 0
signals that all is well; non-zero values usually signal abnormal situations. The exit()
calls fclose
for each open output file, to flush out any buffered output.
Within main, return expr
is equivalent to exit(expr)
. The exit
has the advantage that it can be called from other functions.
The following table presents the difference between break
and exit()
.
break | exit() |
---|---|
|
|
|
|
|
|
No header files needs to be included in order to use |
|
|
|
Example of // some code here before while loop while(true) { ... if(condition) break; } // some code here after while loop In the above code, |
Example of // some code here before while loop while(true) { ... if(condition) exit(-1); } // some code here after while loop In the above code, when |
Conclusively, |
|
Hope you have enjoyed reading differences between break
and exit()
in C. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp