assert函数的作用
转载,原文https://blog.csdn.net/zhu1534120216/article/details/51871736
使用断言
assert()宏是用于保证满足某个特定条件,用法是:
assert(表达式);
如果表达式的值为假,整个程序将退出,并输出一条错误信息。如果表达式的值为真则继续执行后面的语句。
使用这个宏前需要包含头文件assert.h
例如
#include <stdio.h> 
#include <assert.h>
#include <assert.h>
void main() 
{
float a,b;
scan("%f %f",&a,&b);
assert(b!=0);
printf("%f\n",a/b);
}
{
float a,b;
scan("%f %f",&a,&b);
assert(b!=0);
printf("%f\n",a/b);
}
以上的程序要计算A/B的值,因此要求b!=0,所以在程序中使用了assert()用于确保b!=0,如果b==0,则程序会退出。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号