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

How To End A C Program In An “If” Statement

How To End A C Program In An If Statement

C is one of the most popular programming languages and it’s pretty easy to learn and use. Despite this, sometimes we get stuck on simple problems in our C program. Don’t worry, we can teach you! For example, how we can end a program in an C “if” statement? In this post we explain simply how you can end a program with C++ or C Build Tools.

Before we go into this subject though, let’s make one thing clear – this is theoretical. It’s not best practice to exit your conditional statement blocks like this – in fact if you do it when working in a team development environment you may find your code draws some significant criticism and may even fail some coding standards checks! The great thing about C++ is it gives you the freedom to do what you want but as someone else said, with great power comes great responsibility – so use your own judgement and that of your peers on which ways work best for you.

How to end a C program in an if statement with return?

If you want to end your C program in an if statement, just simply you can use return 0;

some compilers may expect you to use return(0) as below,

How can I set a success or failure value when my C program exits?

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. You can use stdlib.h to use return EXIT_SUCCESS; both are same. If this is about failure of your program, then you can use return EXIT_FAILURE;

How to end a C program in an if statement with exit()?

We highly recommend you exit by using return function. If you want to use exit() functions, you need to add stdlib.h header.

How to end a C program in an if statement with abort()?

We highly recommend you exit by using return function. If you want to use abort() function to raise, you need to add stdlib.h header.

How to end a C program in an if statement with a check?

How to end a C program in an if statement with a function check?

Let’s assume we have some process, and we have a conditional statement like so: if a>0, then inside this condition, in some step we want to end program in this if statement. Before we decide to end or not, we should check another condition, so we need another if inside this one. Thus, our function should return int as same as main(),

Let’s assume we have a function or method (a procedure) and we want to end program from there. First our function should be an integer function ( int check(){ … } )and it should check a parameter ( here it is int b )

These both return will not end our app but it returns 0, which means we should and our app with this result.

How to end a C++ program in an if statement?

In C++, we end and exit same as given example below. Instead of getchar() generally system(“pause”) is proffered,

When you want to 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 there is a failure on exit of the application. You can use return EXIT_SUCCESS; or return EXIT_FAILURE; as above example. Here is how to use in an if statement as given example below

This is completely the same as the above.

Professionally, If you are ending your application be sure that you stop all tasks in all CPU cores and you free memory safely from all allocated variables / pointers or any cache you created. You must consider all user ending scenarios when you are ending your application, i.e., ending app by Ctrl+C, Closing Window, ending via Task Manager etc.

If you are looking for C and C++ Compiler for Windows, iOS or Android,

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.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

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++?