Site icon Learn C++

How To Exit From A Program In C On Windows

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.

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

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.

[crayon-66dce4452a3d2502528474/]

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,

[crayon-66dce4452a3da340174781/]

You can use return in the middle or end of your main program to exit from a C program. For example,

[crayon-66dce4452a3db547510030/]

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,

[crayon-66dce4452a3dc722846564/]

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.

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

Exit mobile version