一、assert(断言)简介
assert的功能,条件为真,程序继续执行;如果断言为假(false),则程序终止。
assert是个 宏定义 !
头文件:
#include 原型:
void assert(scalar expression);返回值:无返回值。
头文件assert.h内容如下:
/* copyright (c) 1991-2018 free software foundation, inc. this file is part of the gnu c library. the gnu c library is free software; you can redistribute it and/or modify it under the terms of the gnu lesser general public license as published by the free software foundation; either version 2.1 of the license, or (at your option) any later version. the gnu c library is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. see the gnu lesser general public license for more details. you should have received a copy of the gnu lesser general public license along with the gnu c library; if not, see . *//* * iso c99 standard: 7.2 diagnostics */#ifdef _assert_h# undef _assert_h# undef assert# undef __assert_void_cast# ifdef __use_gnu# undef assert_perror# endif#endif /* assert.h */#define _assert_h 1#include #if defined __cplusplus && __gnuc_prereq (2,95)# define __assert_void_cast static_cast#else# define __assert_void_cast (void)#endif/* void assert (int expression); if ndebug is defined, do nothing. if not, and expression is zero, print an error message and abort. */#ifdef ndebug# define assert(expr) (__assert_void_cast (0))/* void assert_perror (int errnum); if ndebug is defined, do nothing. if not, and errnum is not zero, print an error message with the error text for errnum and abort. (this is a gnu extension.) */# ifdef __use_gnu# define assert_perror(errnum) (__assert_void_cast (0))# endif#else /* not ndebug. */#ifndef _assert_h_decls#define _assert_h_decls__begin_decls/* this prints an assertion failed message and aborts. */extern void __assert_fail (const char *__assertion, const char *__file, unsigned int __line, const char *__function) __throw __attribute__ ((__noreturn__));/* likewise, but prints the error text for errnum. */extern void __assert_perror_fail (int __errnum, const char *__file, unsigned int __line, const char *__function) __throw __attribute__ ((__noreturn__));/* the following is not at all used here but needed for standard compliance. */extern void __assert (const char *__assertion, const char *__file, int __line) __throw __attribute__ ((__noreturn__));__end_decls#endif /* not _assert_h_decls *//* when possible, define assert so that it does not add extra parentheses around expr. otherwise, those added parentheses would suppress warnings we'd expect to be detected by gcc's -wparentheses. */# if defined __cplusplus# define assert(expr) (static_cast ? void (0) : __assert_fail (#expr, __file__, __line__, __assert_function))# elif !defined __gnuc__ || defined __strict_ansi__# define assert(expr) ((expr) ? __assert_void_cast (0) : __assert_fail (#expr, __file__, __line__, __assert_function))# else/* the first occurrence of expr is not evaluated due to the sizeof, but will trigger any pedantic warnings masked by the __extension__ for the second occurrence. the ternary operator is required to support function pointers and bit fields in this context, and to suppress the evaluation of variable length arrays. */# define assert(expr) ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ if (expr) ; /* empty */ else __assert_fail (#expr, __file__, __line__, __assert_function); }))# endif# ifdef __use_gnu# define assert_perror(errnum) (!(errnum) ? __assert_void_cast (0) : __assert_perror_fail ((errnum), __file__, __line__, __assert_function))# endif/* version 2.4 and later of gcc define a magical variable `__pretty_function__' which contains the name of the function currently being defined. this is broken in g++ before version 2.6. c9x has a similar variable called __func__, but prefer the gcc one since it demangles c++ function names. */# if defined __cplusplus ? __gnuc_prereq (2, 6) : __gnuc_prereq (2, 4)# define __assert_function __extension__ __pretty_function__# else# if defined __stdc_version__ && __stdc_version__ >= 199901l# define __assert_function __func__# else# define __assert_function ((const char *) 0)# endif# endif#endif /* ndebug. */#if defined __use_isoc11 && !defined __cplusplus# undef static_assert# define static_assert _static_assert#endifassert的定义如下:
此句意思如下:
expr为真,执行 __assert_void_cast (0) ;否则执行 __assert_fail (#expr, __file__, __line__, __assert_function))条件表达式 ,伪代码:
a?b:c//如果a为真,执行b;//如果a为假,执行c二、测试代码
参数数量为2,则输出参数。否则输出错误信息,并终止程序执行。测试代码如下:
#include #include int main(int argv,char *argc[]){ printf(argv=%dn,argv); assert(argv== 2); printf(argc[1]=%sn,argc[1]); return 0;}
Nanoprecise推出全球首款光能量采集预测性维护传感器
激光打标视觉定位系统的应用优势以及特点
我们该如何辨别AGV小车的质量好坏
都是韩货的三星QLED和LG的OLED屏,你还要吗?
雄安新区将打造绿色智能交通系统,以创新为第一动力
C语言assert(断言)简介
红外线探测器原理_红外线探测器怎么安装
50赫兹交流电容容抗速查表,capacitor reactance @50Hz AC
怎么分析频谱分析仪失锁维修故障?
基于联网式自适应多相位智能型信号机的城市交通灯控制系统的设计
如何使用WireMock搭建一个HTTP模拟器
双路音频信号处理器AK7782的主要特性及应用电路
Windows 7将停止支持,Windows 10份额却下降了
华为正式推出了首款采用升降式前置摄像头的机型华为P Smart Z
一加6T曝光支持5G闪光灯位置变成了夹在两个摄像头中间
天线的极化是电磁波的极化吗?
FP5207配合适MOS管,可实现100-300W大功率
照亮你的美,年轻人的时尚三星GALAXY C9 Pro
除静电离子风机的种类和功能介绍
微星旗舰主机TridentX高清图赏