C++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.

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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Learn C++

How To Run A Program Written In The C Language

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

How To Compile And Run A C Program

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

How To Exit A C Program In Power Shell

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

How To Stop A C Program In Terminal