C标准库的阅读(1)

来源:互联网 发布:sql注入攻击语句 编辑:程序博客网 时间:2024/05/01 09:25

C标准库的阅读(1)
assert.h
1,基本内容
assert.h 里面提供了一个宏 assert(exp) 这个宏指向另一个宏 NDEBUG 如果你在include头文件之前,定义了这个宏 ,那么 assert(exp) 的定义就是 ((void)0) 意思是什么也不做,相当与关闭断言的功能,如果你没有定义这个宏,相当与开启了断言的功能。
2,里面的源码

/* NDEBUG not defined */void _Assert(char *);#define _STR(x) _VAL(x)#define _VAL(x) #x#define assert(test) (test) ? (void)0  : _Assert(__FILE__ ":" _STR(__LINE__) " " #test)/* in xassert.c */#include <assert.h>#include <stdio.h>void _Assert(char *msg) { fprintf(stderr, "%s -- assertion failedn", msg); abort();}

_VAl(X) #X 代表在x的两边加上” 将它字符串化
那么为什么要、
_STR(X) _VAL(X)
_VAL(X) #X
因为 X 可能是一个宏,所以VAL这个宏可能只会把宏的名字给替换成字符串,而不是将宏的内容给替换了。
这就是我看这个源码里面不太懂的地方了