assert关键字使用方法

assert关键字意为断言;
assert宏的原型定义在

#include <assert.h>
void assert( int expression );

 assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。

#include <stdio.h> 
#include <assert.h>
int main()
{
    int a, b;
    scanf("%d   %d", &a, &b);
    assert(b != 0);
    printf("%d\n", a / b);
}

上述代码中是要计算a/b,当b不等于0时这种计算才合法,其中assert(b!=0)处是断言;是当b不等于0时打印出结果,当b等于0时程序退出并输出一个错误信息如下:
这里写图片描述 

猜你喜欢

转载自blog.csdn.net/qq_39539470/article/details/80018302