Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
exit()
2.1.
Syntax
2.1.1.
Example
3.
abort()
3.1.
Syntax
3.1.1.
Example
4.
assert()
4.1.
Syntax
4.1.1.
Example
5.
Frequently Asked Questions
5.1.
What is the use of exit in C?
5.2.
What happens when the abort function is called?
5.3.
What is the use of assert in C?
6.
Conclusion
Last Updated: Mar 27, 2024

exit(), abort(), and assert()

Introduction

One of the most critical aspects of the control flow of a program is the termination of the program. There are several functions and keywords in the C language to terminate the program. In this article, we will be discussing three functions that help us to terminate the program in the C language, which are exit(), abort(), and assert(). We shall be discussing them in detail in the upcoming sections, along with syntax and coding examples for each.

Also Read About, Sum of Digits in C, C Static Function

exit()

The exit() function ends the calling function without executing other processes. Processes are terminated when the exit() method is called. The header file "stdlib.h" declares it. It does not produce any results.

Syntax

void exit(int status_value);
You can also try this code with Online C Compiler
Run Code

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10;
   printf("The value of x: %d\n", x);
   exit(0);
   printf("Calling of exit()");
   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

The value of x: 10
You can also try this code with Online C Compiler
Run Code

In this example, we have used the exit function to end the calling without executing the function.

abort()

The function abort() causes the execution to stop unexpectedly. It is suggested that this function not be used for termination. The header file "stdlib.h" declares it.

Syntax

void abort(void);
You can also try this code with Online C Compiler
Run Code

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int a = 15;
   printf("The value of a: %d\n", a);
   abort();
   printf("Calling of abort()");
   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

The value of a: 15
You can also try this code with Online C Compiler
Run Code

In this example, we have used the abort() method to stop the execution unexpectedly.

assert()

The header file "assert.h" declares the assert() method. It evaluates the arguments passed to it. If the expression is correct, it has no effect. If the expression is false, the execution is aborted.

Syntax

void assert(int exp);
You can also try this code with Online C Compiler
Run Code

Example

#include <stdio.h>
#include <assert.h>
int main() {
   int a = 15;
   printf("The value of a: %d\n", a);
   assert(a!=15);
   printf("Calling of assert()");
   return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Assertion failed: a!=15, file c.c, line 6
The value of a: 15
You can also try this code with Online C Compiler
Run Code

In this example, we have used the assert function to check if the expression is true. The abort function will not work. Otherwise, it will abort and show the error.

Also see, Tribonacci Series and  Short int in C Programming

Frequently Asked Questions

What is the use of exit in C?

The void exit(int status) function in the C library ends the calling process instantly.

What happens when the abort function is called?

The abort function aborts the execution of a currently running process. When the abort function is called, it raises the signal, causing the current process to terminate abnormally.

What is the use of assert in C?

assert is a macro in the C programming language that can be used as a function. It verifies the value of an expression that we would expect to be true under normal conditions.

Conclusion

In this article, we have discussed the exit(), abort(), and assert() functions along with their usage and their syntaxes.

If you want to learn about AR and VR applications, visit Snake Game and want to participate in contests Contests.

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, Javascript, etc.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

For placement preparations, visit interview experiences and interview bundle.

We hope that this blog has helped you in enhancing your knowledge. If you liked this article, please give it a Thumbs Up!

Happy Coding!.

Live masterclass