What the assert function does

Reprinted, the original text https://blog.csdn.net/zhu1534120216/article/details/51871736

 

use assertions
 
The assert() macro is used to ensure that a certain condition is met. The usage is:
 
assert(expression);
 
If the expression evaluates to false, the entire program exits with an error message. If the expression evaluates to true, the execution of the following statement continues.
 
Before using this macro, you need to include the header file assert.h
 
E.g
 
#include <stdio.h>
#include <assert.h>
 
void main()
{
float a,b;
scan("%f %f",&a,&b);
assert(b!=0);
printf("%f\n",a/b);
}
 

The above program needs to calculate the value of A/B, so b!=0 is required, so assert() is used in the program to ensure b!=0, if b==0, the program will exit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607509&siteId=291194637