CC++Learn C++

How To Exit From A Program In C and C++

If you like to develop apps, then C++ programming in Windows with full, modern attractive graphical user interface and the full functionalities of all the available UI elements is very important since Windows users still represent a HUGE market. Despite all the support from the operating system and powerful compilers which spot errors and ‘not responding’ loops, sometimes our C++ apps need to be made to terminate. Alternatively, we sometimes need to indicated that the program has run and exited successfully – or unsuccessfully – from whatever the task the app was undertaking.

How can we exit from a running C or C++ app? How can we exit from a loop in a C++ app but not from the main program? Is return 0; enough to exit successfully from your app? Let’s see with some C++ examples.

How to exit a program in C?

In C and C++ programs we can use return to exit a program. The example below is a “Hello World” C program that can be run with C++ Builder.

In this example above, we print some text with the printf() function and then we wait to get a character input (key press) with the getchar(); function. Then we exit and return 0; which signals to the operating system that our app successfully ran and exited.

How to exit a program in C++

In C++, we exit in the same way as we do in C. Instead of getchar() generally we use system("pause").

When you exit a C++ program you can use EXIT_SUCCESS which means your program has successfully completed which is same as using a zero (0). Or you can use EXIT_FAILURE which means there is a failure on exit of the application, or something did not go as planned. You can use return EXIT_SUCCESS; or return EXIT_FAILURE; as we show in the example below:

The effect is the same as the first example although theconstant values make it a little more readable for people.

How to exit a program in C++ in the early stages?

If you want to exit your main program early, you can use same return command before. For example:

How to exit a program in C++ in a loop?

Let’s consider you have a lot of calculations in a loop, and you want to exit from your loop, and you want to continue to other command lines as below,

If you run this example above and you press Ctrl+C this will end all the application, You will not see the “Safe to exit” prompt and pause operation. How you can exit a C++ loop but not the main()? To do this, you should create a signal handler (i.e. sighandler() ) that changes a parameter check (i.e. loop ). Then you should set this signalhandler using the signal() command as below,

How to exit a program in C++ Builder?

How To Exit From A Program In C and C++ The C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, macOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

How To Exit From A Program In C and C++ The RAD Studio IDE in light mode
The Latest RAD Studio C++ Builder Welcome Screen

You can simply run your application in Debug mode and use STOP button to exit from your C or C++ application.

You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.

Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.

Download RAD Studio 11 Now

See What’s New in RAD Studio 11

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?