assert() function of assert.h in C

In this article, we are going to learn about the use assert() function of assert.h header file in C language, and use it to hold the process till 0 passes as a parameter.
Submitted by Manu Jemini, on April 04, 2018

If you are looking for a function which keeps track of a variable’s value. If the value changes, the function tells about it, otherwise not.

Using assert function comes up mainly during testing, when you want to check for all the variables during the program lifecycle. This can also be useful in debugging, if your program got lots of manipulation and you want to check if value of a variable crosses certain limit or even changes.

To make use of assert just call it with the variable of your choice.

Example:

    Assert (a)
    This will keep track of value of a.

assert.h - assert() function Example in C

#include <stdio.h>
#include <assert.h>
 
int main()
{
	// Defining variables
	int a;
 
	// Assigning value of a=2
	a = 2;
 
	// Displaying the value of a
	printf("Value of a is: %d\n", a);
 
	// assert function will not exit till the value of a =0
	assert(a);
 
	// now change the value of a=0
	a = 0;
 
	// Displaying the value of a
	printf("Value of a is: %d\n\n", a);
 
	// again calling the function with different parameter
	assert(a);
 
	return 0;
 
}
 

Output

assert.h - assert()  in c language



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.