编写代码时,我们总会做出一些假设,断言就是用于在代码处理这些假设的形式。
断言表示相信在程序中的某个在程序中的某个特定点该表达式为真。可以在任何时候启用和禁用断言验证,因此可以在测试时使用断言,
在部署时禁用断言。
1、断言:断定表达式一定为真,如果为假程序崩溃。
2.报告崩溃的情况和位置
#include<stdio.h>
#include<string.h>
#include<assert.h>//需要输入头文件
int Mystrlen(const char *str)
{
assert(str != NULL);//断言必须在if语句之前,否则无法验证
if(str == NULL)
{
return 0;
}
int count = 0;
while(*str != '\0')
{
count++;
str++;
}
return count;
}
断言表示相信在程序中的某个在程序中的某个特定点该表达式为真。可以在任何时候启用和禁用断言验证,因此可以在测试时使用断言,
在部署时禁用断言。
1、断言:断定表达式一定为真,如果为假程序崩溃。
2.报告崩溃的情况和位置
#include<stdio.h>
#include<string.h>
#include<assert.h>//需要输入头文件
int Mystrlen(const char *str)
{
assert(str != NULL);//断言必须在if语句之前,否则无法验证
if(str == NULL)
{
return 0;
}
int count = 0;
while(*str != '\0')
{
count++;
str++;
}
return count;
}