CC++Introduction to C++Learn C++

How To Exit A Program In C++ and C

How To Exit A Program In C++ and C

C++ and C are two of the World’s most powerful programming languages. You use and Windows C++ IDE and compiler to create and build C and C++ programs. Using a fast and reliable C++ compiler for Windows is very important for beginners and professionals since it helps software developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong.

You can run C++ programs by the C++ compilers and build tools like C++ Builder. C++ Builder has free C++ Builder Community Edition and C++ Builder Professional / Architect / Enterprise Versions. 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, along with some examples.

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

  • If you have a running C or C++ application in the Command Prompt, and it is stuck in an endless loop or you want to end this running program, just press Ctrl+C to end the app.
  • If you are running C or C++ app in the IDE, then in all IDEs there is a STOP button to stop the application from running. You can use the PAUSE button to Pause and continue again too.
  • On Windows, if you are not able to end your C++ application, the last chance is to use Task Manager. Right-click to the taskbar, choose Task Manager, and In the Task Manager, find your C++ app listed in the Task Manager and right-click on it then end that task.

How to exit a program in C?

In C and C++ programs we use the return keyword to exit a program in C. If you are a beginner, “Hello World” examples are a good way to understand the basic features of that programming language. This full example below is a “Hello World” C example that can be run with C++ Builder.

In this example above we print a text with printf() function and then we wait to get a character input (key press) with getchar(); functions, sometimes this is necessary. Then we exit and return 0; which means our main app successfully run and exit.

How to exit a program in C++

In C++, we exit shown in the example below. Instead of getchar() generally in C++ the system("pause") command is used.

When you exit a C++ program you can use EXIT_SUCCESS which means your program is successfully completed which is same as 0. Or you can use EXIT_FAILURE which means you want to signal there is a failure or error on exit of the application. You can use return EXIT_SUCCESS; or return EXIT_FAILURE; as given example below.

This is completely same as above, both are same.

How to exit a program in C++ in the early stage of running

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++ while 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 can you exit a C++ loop but not in the main() section? To do this, you should create a signal handler i.e. sighandler() that changes a parameter check. You should set this signalhandler by the signal() command as below.

How to exit a program in C++ Builder

How To Exit 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 A Program In C++ and C The RAD Studio IDE running 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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
Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?