What are the differences between break and exit() in C?

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);
The value of 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().

Difference between break and exit()
break exit()

break is a keyword in C.

exit() is a standard library function.

break causes an immediate exit from the switch or loop (for, while or do).

exit() terminates program execution when it is called.

break is a reserved word in C; therefore it can't be used as a variable name.

exit() can be used as a variable name.

No header files needs to be included in order to use break statement in a C program.

stdlib.h needs to be included in order to use exit().

break transfers the control to the statement follows the switch or loop (for, while or do) in which break is executed.

exit() returns the control to the operating system or another program that uses this one as a sub-process.

Example of break

// some code here before while loop
while(true)
{
   ...
   if(condition)
     break;
}
// some code here after while loop

In the above code, break terminates the while loop and some code here after while loop will be executed after breaking the loop.

Example of exit()

// some code here before while loop
while(true)
{
   ...
   if(condition)
     exit(-1);
}
// some code here after while loop

In the above code, when if(condition) returns true, exit(-1) will be executed and the program will get terminated. Upon call of exit(-1); -1 will be returned to the calling program that is operating system most of the time. The some code here after while loop will never be executed in this case.

Conclusively, break is a program control statement which is used to alter the flow of control upon a specified conditions.

exit() is a libbrary function, which causes immediate termination of the entire program, forcing a return to the operating system.

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!




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

Three apologises after network problemsPosted on Saturday April 22, 2017

The mobile phone company says some customers were unable to send texts or make calls on Saturday.

Brain Tumour Charity cautious about Italy mobile phone rulingPosted on Friday April 21, 2017

Charity urges caution after a ruling in Italy about mobile phone use and brain tumour growth.

US Navy cracks down on sharing of intimate photographsPosted on Friday April 21, 2017

The order comes after an investigation into the sharing of images of female marines on Facebook.

Courtesy BBC News