![pexelssamlion6002000 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2022/09/pexels-sam-lion-6002000-scaled.jpg?resize=1140%2C694&ssl=1)
![pexelssamlion6002000 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2022/09/pexels-sam-lion-6002000-scaled.jpg?resize=1140%2C694&ssl=1)
C is one of the most perennially powerful and popular programming languages. Using a fast and reliable C++ IDE like Dev-C++ or the truly excellent C++ Builder to create your C program or C++ apps is very important for both beginners and professionals alike since it helps developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong. C and C++ app can be developed
Developing a C program 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. The instructions in this article apply to a C++ or C program. So, how we can close a running C++ or C program ?
Table of Contents
How to close a C program when running in terminal?
- 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 close the app. This is default in Windows and Linux console apps, in addition Mac-OS console apps too.
- 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 correctly close a C++ program or app at the end of code?
In C and C++ programs we use return to exit a program in C. If you are a beginner, then “Hello World” examples are good to understand features of the 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 define main() function as a integer (int) function, that means we should return a integer value. Here, we print a text with 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 run and exit.
We highly recommend you exit by using return function. In some compilers, If you want to exit from app you can use exit(); or abort(); functions, you need to add stdlib.h header. If you want to show the exit value in terms of coding, there are two definitions, EXIT_SUCCESS and EXIT_FAILURE
are defined in stdlib.h. Include stdlib.h then;
You can exit from your app as below,
return EXIT_SUCCESS;
If this is about failure of your program then you can exit from your app as below,
return EXIT_FAILURE;
Here is a C example to close your main program,
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { printf("Hello World"); getchar(); return EXIT_SUCCESS; } |
How to correctly close a C program which is executing a loop?
In most C languages you do not need to define main()
program as int. That means you don’t need to return 0. For example, this program will exit automatically,
1 2 3 4 5 6 7 8 |
#include <stdio.h> main() { printf("Hello C Programmer"); getchar(); } |
You can use return in the mid or end of your main program to exit from C program. For example,
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> main() { printf("Hello C Developer"); getchar(); return; } |
The example above has a void main()
function. Some C compilers require the main function to be declared as void
. Again, you do not need to use return; because it is a void function, as in the example below,
1 2 3 4 5 6 7 8 |
#include <stdio.h> void main() { printf("Hello C World"); getchar(); } |
How to close a C program which is running in C++ Builder?
![C++ Builder Common Libraries Run Time Library Parallel Programming Library | Embarcadero RAD Studio Delphi C++Builder Blogs | Learn C++ 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 Common Libraries Run Time Library Parallel Programming Library | Embarcadero RAD Studio Delphi C++Builder Blogs | Learn C++ 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.
![| Learn C++ How To Close A C Program The RAD Studio Welcome window](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/06/RADS11.1U1Light_Welcome-9155681-1024x623.jpg?resize=750%2C456&ssl=1)
![| Learn C++ How To Close A C Program The RAD Studio Welcome window](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/06/RADS11.1U1Light_Welcome-9155681-1024x623.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. If you run your application in release mode and you are not able to stop your app via window close buttons, you must use Task Manager in Windows and you should end your application by the right click and kill process operations.
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.