Assert() Function in C
Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows the standard error in the program using the string parameter to display the failed condition.
Then the variable sets to the exit function as _assert_exit to the one and compiler the exit statement.
In c programming language we use the assert.h header file to invoke the assert () function. This library will be provided by a Marco function which will be used to exit the statement with the standard error.
Assert is a macro function that is used to check the specific conditions at the runtime and it is very useful while debugging the source code or program.
The assert () function is used to tests the condition expression or statement. If the statement is false, it prints the standard error message, the string parameter is used to describe the false condition. Then it sets to the variable like _assert_exit and prints the exit statement.
The assert.h is the header file of the C which we used in the assert function and the Standard Library provides a macro called assert which can be used to verify the assumptions made by the code and print a message if this assumption is not true.
Similarly, we will use other expressions in the programming languages. Rather than int we use the Boolean function and the Boolean expression to check the logical and relatively conditions.
If we check the condition, it returns false it will display the error message and exit from the program or else the program will exit without assertions.
Here, we use the return data type as: void function
As we know that the assert function is a macro function and that macro function doesn't return any value. Hence this is the reason for that the return data type is void.
Assertion violations:
If the assert condition is violated during the compiling and executing of the program. if there is any error or bug present in the code then that function called the precondition. After successful compilation the bug will be found and it will be fixed. This is the concept of precondition.
Whereas in the post condition, if the post condition of the program is violated during the execution or testing of the code. if there is a bug or error present in the code it contains the precondition. Then the bug must be found and it will be fixed while debugging process.
Syntax for the assert () function:
Void assert (int expression)
Example 1:
#include<assert.h>
Let’s see some example programs for assert function in c
Example 1:
Include <stdio.h>
#include <assert.h>
Int main(int argc, const char * argv[])
{
Int pos = 1
Printf(“pos is %d\n”, exp);
Assert(pos);
Pos = 0;
Printf(“Pos is %d\n”, pos);
Assert(pos);
Return 0;
}
Output:
Pos is 1
Pos is 0
Assert: assert.c: main 24
Assertion failed
In the above program we used two header files like stdio.h and assert.h
Then we declared an expression pos and declared that expression. Assert should not exit. since exp is not 0
And changing the expression into 0.
In this case expression is 0 so assert value will display an error and exit
Example 2:
#include <stdio.h>
#include <assert.h>
Int main () {
Int x, y;
Printf(“the two numbers are to be divided\n”);
Scanf(“%d%d”, &x, &y);
Assert (y!= 0);
Printf(“%d/%d = %.2f\n”, x, y, x/(float)y);
Return 0;
}
Output:
The two numbers going to be divided:
4,0
Assertion failed: (y!=0), function main, file assert.c, line 10
Abort trap: 4
In the above program the assert occurred because of 4 can’t divided by 0.
Thus, the assertion occurred at line 10.
Example 3:
#include <assert.h>
#include <stdio.h>
Int main () {
Int x;
Char str [10];
Printf(“Enter the value: “);
Scanf(“%d”, &x);
Assert (x >= 10);
Printf(“value entered is %d\n”, x);
Printf(“Enter string: “);
Scanf(“%s”, str);
Assert (str!= NULL);
Printf(“String entered is: %s\n”, str);
Return (0);
}
Output:
Enter the value: 1
Value entered is 1
Enter string: java point
String entered is: java point
This is the basic assert program without assertions. For the beginners this program will be very useful for learning the concept of assert () function.