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:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more