-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 初识断言 (Assertion) | ||
|
||
## 1. 什么是断言 | ||
|
||
断言是用于进行 **调试** 和 **错误处理** 的工具。 | ||
|
||
它允许程序员在代码中插入条件检查,以确保程序在运行时满足特定的前提条件。 | ||
|
||
如果断言的条件不成立,程序将中止执行并生成一条错误消息,提供关于出错位置和原因的信息。 | ||
|
||
## 2. 使用断言 | ||
|
||
```c | ||
#include <assert.h> | ||
|
||
int main(void){ | ||
int a = -5; | ||
assert(a > 0); | ||
} | ||
``` | ||
程序中止执行,因为断言的条件 `a > 0` 不成立。 | ||
## 3. 用途 | ||
用于 **防御性编程**,缩小错误可能存在的范围,便于调试。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters