Skip to content

Commit ce125cf

Browse files
committed
refine content of assert and const
1 parent 360192d commit ce125cf

File tree

3 files changed

+78
-86
lines changed

3 files changed

+78
-86
lines changed

basic_content/assert/README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010

1111
## 1.第一个断言案例
1212

13-
断言,**是宏,而非函数**。assert 宏的原型定义在 <assert.h>(C)、<cassert>(C++)中,其作用是如果它的条件返回错误,则终止程序执行。可以通过定义 NDEBUG 来关闭 assert,但是需要在源代码的开头,include <assert.h> 之前。
13+
断言,**是宏,而非函数**
14+
15+
assert 宏的原型定义在 <assert.h>(C)、<cassert>(C++)中。其作用是如果它的条件返回错误,则终止程序执行。
16+
17+
可以通过定义 `NDEBUG` 来关闭 assert,**但是需要在源代码的开头,include <assert.h> 之前。**
1418

1519
```c
1620
void assert(int expression);
1721
```
18-
对应代码:[assert.c](./assert.c)
22+
23+
> 代码样例:[assert.c](./assert.c)
1924
```c
2025
#include <stdio.h>
2126
#include <assert.h>
@@ -35,22 +40,22 @@ int main()
3540
3641
return 0;
3742
}
38-
3943
```
4044
输出:
4145
```c
42-
assert: assert.c:13: main: Assertion `x==7' failed.
46+
assert: assert.c:13: main: Assertion 'x==7' failed.
4347
```
4448
可以看到输出会把源码文件,行号错误位置,提示出来!
4549

4650
## 2.断言与正常错误处理
4751

48-
断言主要用于检查逻辑上不可能的情况。例如,它们可用于检查代码在开始运行之前所期望的状态,或者在运行完成后检查状态。与正常的错误处理不同,断言通常在运行时被禁用
52+
+ 断言主要用于检查逻辑上不可能的情况。
4953

50-
忽略断言:
54+
>例如,它们可用于检查代码在开始运行之前所期望的状态,或者在运行完成后检查状态。与正常的错误处理不同,断言通常在运行时被禁用。
5155
52-
在代码开头加上:
56+
+ 忽略断言,在代码开头加上:
5357
```c++
5458
#define NDEBUG // 加上这行,则 assert 不可用
5559
```
56-
对应学习的代码:[ignore_assert.c](./ignore_assert.c)
60+
61+
> 样例代码:[ignore_assert.c](./ignore_assert.c)

basic_content/assert/ignore_assert.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
* @date 2019-07-25
77
*/
88

9-
# define NDEBUG
9+
# define NDEBUG // 忽略断言
1010

1111
#include<assert.h>
1212

13-
14-
1513
int main(){
1614
int x=7;
1715
assert(x==5);

0 commit comments

Comments
 (0)