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 problems, top 100 SQL problems, resources, 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!.