Open In App

Return Statement vs Exit() in main() in C++

Improve
Improve
Like Article
Like
Save
Share
Report

The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of <stdlib.h> that is used to terminate the process explicitly.

The operation of the two may look different but in the case of the main() function, they do the same task of terminating the program with little difference. In this article, we will study the difference between the return statement and exit() function when used inside the main() function.

exit() Function in main()

When exit() is used to exit from the program, destructors for locally scoped non-static objects are not called. The program is terminated without destroying the local objects.

Example of exit() in main()

C++




// C++ program to illustrate the
// working of exit() in main()
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
  
using namespace std;
  
class Test {
public:
    Test() { printf("Inside Test's Constructor\n"); }
  
    ~Test()
    {
        printf("Inside Test's Destructor");
        getchar();
    }
};
  
// Driver code
int main()
{
    Test t1;
  
    // using exit(0) to exit from main
    exit(0);
}


Output

Inside Test's Constructor

As we can see in the above example, the destructor of the local object is not called before the program’s termination.

One thing to note here is that the static objects will be cleaned up even when we call exit(). For example, see the following program:

Example of exit() in main() for Static Objects

C++




// C++ Program to illutrate the distruction of static
// objects while using exit()
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
  
using namespace std;
  
class Test {
public:
    Test() { printf("Inside Test's Constructor\n"); }
  
    ~Test()
    {
        printf("Inside Test's Destructor");
        getchar();
    }
};
  
// driver code
int main()
{
    static Test t1; // Note that t1 is static
  
    exit(0);
}


Output

Inside Test's Constructor
Inside Test's Destructor

return Statement in main()

When the return statement is used inside the main() function, the program control is transferred to the calling function, which in this case is the Operating System. So, using a return statement in main() terminates the program execution which is similar to the operation of the exit() function.

Example of return in main()

C++




// C++ Program to illustrate the working 
// of return in main()
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
  
using namespace std;
  
class Test {
public:
    Test() { printf("Inside Test's Constructor\n"); }
  
    ~Test() { printf("Inside Test's Destructor\n"); }
};
  
// driver code
int main()
{
    Test t1;
    static Test t2;
  
    // using return 0 to exit from main
    return 0;
}


Output

Inside Test's Constructor
Inside Test's Constructor
Inside Test's Destructor
Inside Test's Destructor

As we can see, all the objects’ (local or static) destructor is called before exiting the program which was not the case with the exit() function.

Difference between return Statement and exit() Function

The differences between the return Statement and exit() Function are as follows:

return Statement

exit() Function

The return statement is a keyword. exit() is a function.
It is part of the language. It is part of the Standard Library <stdlib.h>
It is used to return the program control to the calling function.  It is used to terminate the current process.
When the return is used in main(), all the objects are destroyed whether they are local or static. When the exit() is used, only the destructor of static objects is called.


Last Updated : 06 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads