当前位置:   article > 正文

c标准函数库--->assert.h_#include属于哪个库的

#include属于哪个库的



assert.h是C标准函数库中的头文件。其中定义了assert()宏用于程序调试。

在C标准函数库中,它是个非常特别的头文件,你可以将它引入数次以获得不同的效果,此效果依引入时是否以定义NDEBUG而定。

 

assert()是一个诊断宏,用于动态辨识程序的逻辑错误条件。其原型是: void assert( int expression);

如果宏的参数求值结果为非零值,则不做任何操作(no action);如果是零值,用宽字符(wide characters)打印诊断消息,然后调用abort()。诊断消息包括:

  • 源文件名字 (在stdlib.h中声明的宏__FILE__的值)
  • 所在的源文件的行号(在stdlib.h中声明的宏__LINE__的值)
  • 所在的函数名 (在stdlib.h中声明的宏__func__的值),这是 C99新增的特性
  • 求值结果为0的表达式

诊断信息的显示目标依赖与被调用程序的类型。如果是控制台程序,诊断信息显示在stderr设备;如果是基于窗口的程序,assert()产生一个Windows MessageBox来显示诊断信息。

程序可以屏蔽掉所有的assert()而无需修改源代码。这只需要在命令行调用C语言的编译器时添加宏定义的命令行选项,定义DNDEBUG宏;也可以在源程序程序引入<assert.h>之前就使用#define NDEBUG来定义宏。被屏蔽的assert()甚至不对传递给它的参数表达式求值,因此使用assert()时其参数表达式不能有副作用(side-effects).

 

例程

  1. #include <stdio.h>
  2. #include <assert.h>
  3. int main (void)
  4. {
  5. FILE *fd;
  6. fd = fopen ("/home/user/file.txt", "r");
  7. assert (fd);
  8. fclose (fd);
  9. return 0;
  10. }


例程:

  1. //没有定义NDEBUG
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. int main()
  6. {
  7. printf("1 ok hello \n");
  8. assert(1==4);
  9. printf("2 ok exit \n");
  10. return 0;
  11. }
  1. 结果:
  2. **************************************************************************************************
  3. 1 ok hello
  4. assert_h_ex_nodebug: assert_h_ex.c:7: main: Assertion `1==4' failed.
  5. 已放弃
  6. **************************************************************************************************
  1. //定义NDEBUG
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define NDEBUG
  5. #include<assert.h>
  6. int main()
  7. {
  8. printf("1 ok hello \n");
  9. assert(1==4);;
  10. printf("2 ok exit \n");
  11. return 0;
  12. }
  1. 结果:
  2. ********************************************************************************************************************************
  3. 1 ok hello
  4. 2 ok exit
  5. ********************************************************************************************************************************
  1. 原理:
  2. #define assert(test) if(!(test))\
  3. fprintf(stderr,"the failed : %s file %s ,line %i\n",#test, __FILE__,__LINE__);\
  4. abort();
  1. 模拟:
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. //#define NDEBUG
  5. //#include<assert.h>
  6. #define Assert(test) if(!(test)) fprintf(stderr,"Assertion failed: %s, file %s, line %i\n", #test, __FILE__, __LINE__);abort()
  7. int main()
  8. {
  9. printf("1 ok hello \n");
  10. Assert(1==4);
  11. printf("2 ok exit \n");
  12. return 0;
  13. }
  1. 结果:
  2. *************************************************************************************************
  3. 1 ok hello
  4. Assertion failed: 1==4, file assert_h_ex.c, line 9
  5. 已放弃
  6. **************************************************************************************************

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/721834
推荐阅读
相关标签
  

闽ICP备14008679号