Exit codes in C/C++ with Examples (original) (raw)

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:

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 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 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 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: