Exit codes in C/C++ with Examples
Last Updated :
15 Jul, 2025
The purpose of the exit() function is to terminate the execution of a program. The "return 0"(or EXIT_SUCCESS) implies that the code has executed successfully without any error. Exit codes other than "0"(or EXIT_FAILURE) indicate the presence of an error in the code. Among all the exit codes, the codes 1, 2, 126 - 165 and 255 have special meanings and hence these should be avoided for user-defined exit codes.
Syntax
void exit(int return_code)
Note: It is also to taken into consideration that an exit code with a value greater than 255 returns an exit code modulo 256.
For Example: If we execute a statement exit(9999) then it will execute exit(15) as 9999%256 = 15.
Some of the Exit Codes are:
- exit(1): It indicates abnormal termination of a program perhaps as a result a minor problem in the code.
- exit(2): It is similar to exit(1) but is displayed when the error occurred is a major one. This statement is rarely seen.
- exit(127): It indicates command not found.
- exit(132): It indicates that a program was aborted (received SIGILL), perhaps as a result of illegal instruction or that the binary is probably corrupt.
- exit(133): It indicates that a program was aborted (received SIGTRAP), perhaps as a result of dividing an integer by zero.
- exit(134): It indicates that a program was aborted (received SIGABRT), perhaps as a result of a failed assertion.
- exit(136): It indicates that a program was aborted (received SIGFPE), perhaps as a result of floating point exception or integer overflow.
- exit(137): It indicates that a program took up too much memory.
- exit(138): It indicates that a program was aborted (received SIGBUS), perhaps as a result of unaligned memory access.
- exit(139): It indicates Segmentation Fault which means that the program was trying to access a memory location not allocated to it. This mostly occurs while using pointers or trying to access an out-of-bounds array index.
- exit(158/152): It indicates that a program was aborted (received SIGXCPU), perhaps as a result of CPU time limit exceeded.
- exit(159/153): It indicates that a program was aborted (received SIGXFSZ), perhaps as a result of File size limit exceeded.
Hence the various exit codes help the user to debug the code. For instance, if one receives an exit code of 139, this implies that the code has a segmentation fault and the code can be debugged accordingly.
Program 1:
Below program will give Segmentation Fault:
CPP
// C++ program to demonstrate Segmentation Fault
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// An array of size 100
int arr[100] = { 0 };
// When we try to access the array out
// of bound, it will give Segmentation Fault
cout << arr[100001];
return 0;
}
Output:
Below is the output of the above program:
Program 2:
Below program will give Floating Point Error:
CPP
// C++ program to demonstrate
// Floating Point Error
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int a = 1, b = 0;
// When we try to divide by zero
// it should give SIGFPE
cout << a / b;
return 0;
}
Output:
Below is the output of the above program:
Program 3:
Below program will give Time Limit Exceed:
CPP
// C++ program to demonstrate TLE
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Below statement will give time
// limit exceeded as well as memory
// limit exceeded due to infinite loop
for (;;) {
int arr[10000];
}
return 0;
}
Output:
Below is the output of the above program:
Similar Reads
exit(0) vs exit(1) in C/C++ with Examples exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. There are two types of exit status in C/C++: Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been
2 min read
exit(0) vs exit(1) in C/C++ with Examples exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. There are two types of exit status in C/C++: Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been
2 min read
exception::bad_exception in C++ with Examples Standard C++ contains several built-in exception classes, exception::bad_exception is one of them. This is an exception thrown by unexpected handler. Below is the syntax for the same: Header File: include<exception> Syntax: class bad_exception; Return: The exception::bad_exception returns a nu
2 min read
exit() vs _Exit() in C/C++ exit() and _Exit() in C/C++ are very similar in functionality. However, there is one difference between exit() and _Exit() and it is that exit() function performs some cleaning before the termination of the program like connection termination, buffer flushes, etc. exit() In C, exit() terminates the
3 min read
What does main() return in C and C++? C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t
3 min read
C++ Programming Examples Writing C++ programs yourself is the best way to learn the C++ language. C++ programs are also asked in the interviews. This article covers the top practice problems for basic C++ programs on topics like control flow, patterns, and functions to complex ones like pointers, arrays, and strings.Basic C
7 min read