exit() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The exit() method is an inbuilt method of the cstdio library that is used to terminate the flow of execution of the c++ program.

The exit() method deallocate all the memory that is occupied by the c++ program before the termination of the c++ program.

Syntax of exit in c++

We can use the exit() method in the main() method or any kind of user-defined method, where we want to terminate the flow of execution of the c++ program without any kind of error.

Let's understand the syntax of the exit() method in C++.

Syntax:

Parameters of exit() in C++

The exit() method takes one parameter i.e the exit_code that represents the termination status of the c++ program, whether it is successfully terminated or not.

  • The exit_code 0 represents the program is successfully terminated without any kind of an error. A successful termination status is returned to the host environment.
  • The exit_code 1 represents the program is not terminated successfully due to some runtime issues in the c++ program using the bad programming.

Return Value of exit() in C++

The exit() method doesn't return any kind of value.

Exceptions for exit() in C++

The exit() method never throws any kind of exception.

Example

Let's understand the exit() method using an example. In the below example, we are simply terminating the flow of execution of the c++ program using the exit() method.

Output:

The second number is equal to 0 that's why the flow of execution of the program terminates after the printing statement in the condition.

What is exit() in C++?

Let's suppose you want to terminate the program as soon as you get your required result so that you can save time for the execution of the program. How you can perform this operation?

The exit() method of an inbuilt library cstdio is used to stop the execution of the program according to the provided status code as a parameter. The type of parameter is already discussed in the above section of the article.

Flow Diagram of the exit() Method

The exit() method skips all the following statements and terminates the program execution.

Let's suppose, you want to terminate your c++ program when a particular condition gets fails, the below diagram depicts the execution flow of the program.

The exit() function skips all the following statements and terminates the executing program according to the status_code provided by the programmer.

exit in c++

More Example

Let's understand the usage of the exit() method using an example.

In the below program, we are using the exit() method, when the entered number by the user is divisible by the particular number.

Output:

Difference between Return and exit() Method

The main difference between the return and the exit() method is the return statement returns the control of the flow of execution to the function statement, where the function has been called. The return statement may or may not terminate the whole c++ program.

Conclusion

  • The exit() method is a method of cstdio library.
  • The exit() method takes only a parameter and doesn't return any kind of value.
  • The exit() method takes 0 and 1 as a parameter that represents the status of the exit() method.