Difference between break and exit() in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

The break and exit both are jump statements, but there is much difference between break and exit.

Also see:- break statement in C

The break statement is a jump control statement that controls the flow of the execution of the program. When break keyword is encountered inside a loop then control of the program came from the loop and remaining statements of the program are executed.

The exit is a pre-defined standard library function which is defined in header file stdlib.h. The exit function forces an unconditional and immediate termination of the program.

The prototype of the exit function is:-
void exit(int value);

 #include<stdio.h>
 int main()
 {
   for(int i=0; i<5; i++)
   {
     if(i==3) break;
     printf("%d\t",i);
   }
   printf("\nExecution of For loop completed.");
   return 0;
 }

Output:-

0 1 2
Execution of For loop completed

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
   for(int i=0; i<5; i++)
   {
     if(i==3) exit(0);
     printf("%d\t",i);
   }
   printf("\nExecution of For loop completed");
   return 0;
 }

Output:-

0 1 2

Observe the differences between the above two programs, in the first program the break statement is used but in the second program exit function is used. The break statement terminates the loop so, the printf statement is executed but the exit function terminates the whole program so, the statement after exit function doesn’t execute.

Other difference between break and exit

1. The break statement can only be used within the control statements but exit function can be used anywhere in the program.

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
   printf("Hello");
   exit(0);
   printf("Hi");
   return 0;
 }

Output:-

Hello

2. The break is a reserved word in C programming language so, it can’t be used as an identifier in the C program but the exit is a function not a reserved word so, it can be used as the variable name.

 #include<stdio.h>
 int main()
 {
   int exit = 9;
   printf("exit = %d",exit);
   return 0;
 }

Output:-

exit = 9

3. In a C program, more than one break statement can be used and execute but exit function can be used more than once but executes only once.

 #include<stdio.h>
 int main()
 {
   for(int i=1; i<5; i++)
   {
     if(i==3) break;
     printf("i = %d \t",i);
   }
   printf("\n");
   for(int j=9; j>0; j--)
   {
     if(j==5) break;
     printf("j = %d \t",j);
   }
   return 0;
 }

Output:-

i = 1 i = 2
j = 9 j = 8 j = 7 j = 6

In the above program, two break statements are used and both are executed. The below program is similar to the above program, the only difference is we used exit() function instead of a break statement. The exit function terminates the program. So, we can use more than one exit function in a program but among them, only one exit() function can be executed in a program.

 #include<stdio.h>
 #include<stdlib.h>
 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) exit(0);
     printf("j = %d \t",j);
   }
   return 0;
 }

Output:-

i = 1 i = 2

Summary

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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply