![pexelspolinazimmerman3746939 | Embarcadero RAD Studio Delphi C++Builder Blogs pexels polina zimmerman 3746939 2](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/06/pexels-polina-zimmerman-3746939-2844298-scaled.jpg?resize=1140%2C694&ssl=1)
C++ is one of the most perennially powerful and popular programming languages. Using a fast and reliable compiler to create your C++ app is very important for both beginners and professionals alike since it helps C++ developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong.
But writing a C++ app is more than just the skill of getting the app to run in the first place. It’s important to know how to make the app behave properly within the environment of the computer or device on which it is running. Proper behavior isn’t just about how and where we read and store data but also slightly less obvious things like making sure our app terminates properly under various conditions. So, how we can end a running C or C++ app? How can we end app from a loop in a C++ app but not from the main program? Is return 0;
enough to end your C++ app successfully? Let’s take a look at some examples.
Table of Contents
How to properly end a C++ app or C program?
- If you run your C or C++ app in Command Prompt, and it is in 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 C++ IDE, there is a STOP button to stop the application from running. You can use the PAUSE button to Pause and continue too.
- In Windows, if you are not able to end your C++ application, the last chance is to use Task Manager. Right-click on the taskbar – or Start button in Windows 11 and then choose Task Manager. In the Task Manager, find your C++ app listed and right-click on it then select “end task”.
How to end a program using C code?
In C and C++ programs we use return to end a program in C. If you are a beginner “Hello World” examples are good to understand feature of that programming language. It is good to understand how to edit text, how to write in its format, how to compile and link, how to debug and execute, how to deploy or release. This full example below is a “Hello World” C example that can be run with C++ Builder.
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { printf("Hello World"); getchar(); return 0; } |
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 properly end a program in a C++ app?
In C++, we end and exit same as given example below. Instead of getchar() generally system(“pause”) is proffered,
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main() { std::cout << "Hello World"; system("pause"); return 0; } |
When you a C++ program you can use EXIT_SUCCESS
which means your programm is succesfully completed which is same as 0. Or you can use EXIT_FAILURE
which means there is a failure on exit of the application. You can use return EXIT_SUCCESS; or return EXIT_FAILURE;
as given example below
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main() { std::cout << "Hello World"; system("pause"); return EXIT_SUCCESS; } |
This is completely same as above, both are same.
How to correctly end a program in a C++ app in the early stage of the app running?
If you want to end and exit your main program early you can use same return command before. For example,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> int main() { return EXIT_SUCCESS; std::cout << "Safe to exitn"; system("pause"); return EXIT_SUCCESS; } |
How to correctly end a C++ app which is executing a loop?
Let’s consider you have a lot of calculations in a loop, and you want to end and exit from your loop and you want to continue to other command lines as below,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> int main() { while (true){}; std::cout << "Safe to exitn"; system("pause"); return 0; } |
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 the main()
? Todo this, you should create a signal handler i.e. sighandler() that changes a parameter check i.e. loop. Then you should set this signalhandler by the signal()
command as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <signal.h> volatile sig_atomic_t loop; void sighandler(int snum) { loop = 1; } int main() { signal(SIGINT, sighandler); while (!loop){}; std::cout << "Safe to exitn"; system("pause"); return 0; } |
How to end a C++ app which is running in C++ Builder?
![C++ Builder Common Libraries Run Time Library Parallel Programming Library | Embarcadero RAD Studio Delphi C++Builder Blogs | Embarcadero RAD Studio Delphi C++Builder Blogs cbuider studio final icons 64 9744851 4964591 6127446 4112068 5843153](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/06/CBUIDER_STUDIO_FINAL_ICONS_64-9744851-4964591-6127446-4112068-5843153.png?w=750&ssl=1)
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. C++ Builder comes with Rapid Application Development Studio, also known as RAD Studio, and C++ Builder is one of the most professional IDE’s that work under RAD Studio. It is the oldest IDE (it began as Borland TurboC in 1990 and was later renamed Borland C++ Builder). Under the Embarcadero brand, multiple new versions have been released, year upon year, along with great new features, updates, and support to ensure that your apps can stay current. 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.
![RADS111U1LightWelcome9155681 | Embarcadero RAD Studio Delphi C++Builder Blogs rads11 1u1light welcome 9155681](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/06/RADS11.1U1Light_Welcome-9155681.jpg?resize=750%2C456&ssl=1)
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
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition