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.
Table of Contents
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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int a = 1; // Do first processes if(a > 0) { return 0; } // skipped processes getchar(); return 0; } |
some compilers may expect you to use return(0)
as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int a = 1; // Do first processes if(a > 0) { return(0); } // skipped processes getchar(); return(0); } |
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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> #include <stdlib.h> int main() { int a = 1; // Do first processes if(a > 0) { return EXIT_SUCCESS; } // skipped processes getchar(); return EXIT_SUCCESS; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <stdlib.h> int main() { int a = 1 ; // Do first processes if( a>0 ) exit(0); // skipped processes getchar(); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <stdlib.h> int main() { int a = 1 ; // Do first processes if( a>0 ) abort(0); // skipped processes getchar(); return 0; } |
How to end a C program in an if statement with a check?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main() { int a = 1 , b = 1; // Do first processes if( a > 0 ) { // lets check b and end program if necessary if( b == 1 ) return 0; } // skipped processes getchar(); return 0; } |
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 )
1 2 3 4 5 6 7 |
bool check(int b) { // ... if(b == 1) return true; // ... return false; } |
These both return will not end our app but it returns 0, which means we should and our app with this result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> bool check(int b) { // ... if(b == 1) return true; // ... return false; } int main() { int a = 1 , b = 1; // Do first procceses if( a>0 ) { // lets use check function to end program if neccesary if( check(b) ) return 0; } // skipped processes getchar(); return 0; } |
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,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> int main() { int a = 1; if (a>1) return 0; system("pause"); return 0; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { int a = 1, b = 1; if(a>0) { if (b>0) return EXIT_SUCCESS; } system("pause"); return EXIT_SUCCESS; } |
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.