C is one of the most powerful programming languages. There are a great selection of C++ windows development tools, IDE’s and C++ compilers which can also work with C programs. Using a fast and reliable C or C++ compiler for Windows is very important for beginners and professionals since it helps C/C++ developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong.
Put simply, you can run C and C++ code 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. So, how we can exit from a running C or C++ app? How we can exit from a loop in a C or C++ app but not from the main program? Is return 0;
enough to exit successfully from your app? Let’s see it all with a few examples.
Table of Contents
How to exit a running C or C++ program?
- If you have C or C++ application running in the Command Prompt or terminal, 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 a C or C++ app in the IDE, In all IDEs, there is a STOP button to stop the application from running. You can use the PAUSE button to Pause and continue the apps too.
- In 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 properly exit a program in C?
In C and C++ programs we use return
to exit a program in C. Here’s an example below which is a “Hello World” C example that can be run with C++ Builder.
In this example above we define main() function as a integer (int) function, that means we should return a integer value. Here, we print a text with the printf()
function and then we wait to get a character input (key press) with getchar();
function. Sometimes this is necessary to see results when running it. Then we exit and return 0;
which means our main app successfully ran and exited with no errors.
In most C languages you do not need to define the main() program as int. That means you don’t need to return 0. For example, this program will exit automatically,
You can use return in the middle or end of your main program to exit from a C program. For example,
The example above is a void main()
function. Some C compilers require the main function to be defined as void
. Again, you do not need to use return; because it is a void function, as in example below,
How to exit a program in C++ Builder?
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 RAD Studio and the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
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.