exit(0) vs exit(1) in C/C++ with Examples

C/C++ exit(0) vs exit(1): Here, we are going to learn about the exit(0) and exit(1) function with their usages, syntax, examples, and differences between them.
Submitted by IncludeHelp, on June 18, 2020

exit() is a library function in C/C++ programming language, it is used to terminate the calling process (function) immediately i.e. we can say it is used for the normal program termination and also perform the several cleanup steps.

Syntax:

void exit(int status);

Parameter(s): status – defines the exit status.

There are two status that we can define,

Value Macro Description
0 EXIT_SUCCESS It defines that the termination is on the success of the program.
1 EXIT_FAILURE It defines that the termination is on the failure of the program.

Return value: The return type of the function is void, it returns nothing.

1) exit(0) or exit(EXIT_SUCCESS)

exit(0) is used to terminate the program by specifying the successful execution of the program.

Syntax:

exit(0);

2) exit(1) or exit(EXIT_FAILURE)

exit(1) is used to terminate the program by specifying the successful execution of the program.

Syntax:

exit(1);

Example 1:

Here, we are reading two numbers and then dividing them, if the second number is 0 then exiting the program using exit(1) and if the second number is not 0 then performing the division operation and exiting the program using exit(0).

// C++ program to demonstrate the
// example of exit(0) and exit(1)

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;

    cout << "Enter divisor  (value of a): ";
    cin >> a;
    cout << "Enter dividend (value of b): ";
    cin >> b;

    cout << "a: " << a << endl;
    cout << "b: " << b << endl;

    if (b == 0) {
        cout << "Dividend must not be 0..." << endl;
        // EXIT_FAILURE
        exit(1);
    }

    cout << a << "/" << b << " : " << (float)a / (float)b << endl;

    // EXIT_SUCCESS
    exit(0);
}

Output:

RUN 1:
Enter divisor  (value of a): 10
Enter dividend (value of b): 3
a: 10
b: 3
10/3 : 3.33333

RUN 2:
Enter divisor  (value of a): 10
Enter dividend (value of b): 0
a: 10
b: 0
Dividend must not be 0...

Example 2:

Here, we are opening a file in read-only mode, if the file doe not exit then exiting the program using exit(1), and file exists then closing it and exiting the program using exit(0).

// C++ program to demonstrate the
// example of exit(0) and exit(1)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* fPtr;

    // opening the file
    fPtr = fopen("hello1.txt", "r");

    // checking whether file exists or not
    if (fPtr == NULL) {
        printf("oops, file does not exist...");

        // EXIT_FAILURE
        exit(1);
    }

    printf("File opened successfully...");
    // clsoing the file
    fclose(fPtr);

    // EXIT_SUCCESS
    exit(0);
}

Output:

oops, file does not exist...

Difference between exit(0) and exit(1)

exit(0) exit(1)
It is used to terminate the program normally by specifying that operation is successful. It is also used to terminate the program normally by specifying that operation is not successful.
The syntax is,
exit(0);
The syntax is,
exit(1);
0 is the value of EXIT_SUCCESS. Thus, we can also use exit(EXIT_SUCCESS) instead of exit(0). 1 is the value of EXIT_FAILURE. Thus, we can also use exit(EXIT_FAILURE) instead of exit(1).
exit(0) is fully portable. exit(1) is not fully portable.
exit(0) defines the clean exit without any error. exit(1) defines that there was an error in the program and the program is terminated.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.