0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

C语言assert(断言)简介

CHANBAEK 来源: 嵌入式学习和实践 作者: 嵌入式学习和实践 2023-11-17 16:33 次阅读

一、assert(断言)简介

assert的功能,条件为真,程序继续执行;如果断言为假(false),则程序终止。

assert是个 宏定义

头文件:

#include < assert.h >

原型:

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
   < http://www.gnu.org/licenses/ >.  */


/*
 *  ISO C99 Standard: 7.2 Diagnostics  < assert.h >
 */


#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 < features.h >


#if defined __cplusplus && __GNUC_PREREQ (2,95)
# define __ASSERT_VOID_CAST static_cast< void >
#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
#endif

assert的定义如下:

图片

此句意思如下:

expr为真,
执行 __ASSERT_VOID_CAST (0)  ;
否则执行 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))

条件表达式 ,伪代码:

a?b:c
//如果a为真,执行b;
//如果a为假,执行c

二、测试代码

参数数量为2,则输出参数。否则输出错误信息,并终止程序执行。测试代码如下:

#include < stdio.h >
#include < assert.h >


int main(int argv,char *argc[])
{
    printf("argv=%dn",argv);
    assert(argv== 2);
    printf("argc[1]=%sn",argc[1]);
    return 0;
}

图片

图片

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • C语言
    +关注

    关注

    180

    文章

    7533

    浏览量

    128817
  • 程序
    +关注

    关注

    114

    文章

    3631

    浏览量

    79553
  • 代码
    +关注

    关注

    30

    文章

    4556

    浏览量

    66810
收藏 人收藏

    评论

    相关推荐

    C语言assert的使用

    assert意思是断言,常用在程序的DEBUG版本中。
    发表于 07-21 14:51 517次阅读

    什么是断言?C语言中断言的语法和用法

    在软件开发过程中,我们经常需要处理各种错误和异常情况。为了提高代码的健壮性和可靠性,我们需要使用一些工具和技术来检测和处理这些问题。本篇博客将深入探讨C语言中断言的使用,帮助读者更好地理解和应用断言,提高代码的质量和可维护性。
    发表于 08-03 10:34 1675次阅读

    解析C语言断言函数的使用

    对于断言,相信大家都不陌生,大多数编程语言也都有断言这一特性。简单地讲,断言就是对某种假设条件进行检查。 在 C 语言中,
    发表于 08-08 09:51 236次阅读
    解析C<b class='flag-5'>语言</b><b class='flag-5'>断言</b>函数的使用

    如何在if和assert中做选择

    ,而赋值给 g_state 之后,必须保证赋值结果的正确性,因此使用 assert 断言。 五、总结这篇文章分析了 C 语言中比较晦涩、模糊的一个概念,似乎有点虚无缥缈,但是的确又需要
    发表于 04-08 06:13

    断言ASSERT)的用法

    STM32中经常出现assert函数,网上看了篇博客分享下:我一直以为assert仅仅是个报错函数,事实上,它居然是个宏,并且作用并非“报错”。  在经过对其进行一定了解之后,对其作用及用法有了一定
    发表于 08-23 09:33

    如何在XC8中使用断言的?

    大家好,我正在尝试使用XC8中的断言,但是当我使用“*”时,“断言h”空格main(空隙){BOOL X=0;断言(x= 1);而(1){}}我的程序停止,并且在控制台中不显示任何MsAGAGEM
    发表于 03-26 10:58

    C语言中断言如何去使用

    文章目录1 C语言中断言的使用1.1 处理方式1.2 原型定义1.3 示例代码1 C语言中断言的使用1.1 处理方式如果断言的条件返回错误,
    发表于 07-14 08:15

    C语言中断言是怎样使用的?

    C语言中断言是怎样使用的?
    发表于 10-14 07:18

    何为断言断言该怎么使用呢

    存在错误。因此,断言是提高程序可靠性的有效手段。也是开发阶段快速定位问题的一种很好防御式编程方法。在C语言中,断言是一些条件判断的宏。比如C
    发表于 09-21 14:59

    ASSERT的定义及调试技巧

    C语言中的ASSERT(断言)宏是嵌入式软件开发人员可以使用的最好的调试工具之一。
    的头像 发表于 12-17 11:26 4429次阅读

    怎么理解Assert中的断言语句?

    为什么项目中的代码需要有Assert断言语句?
    的头像 发表于 03-03 14:12 2476次阅读

    STM32函数库Assert断言机制

    编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式。断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真。可以在任
    发表于 02-08 15:29 2次下载
    STM32函数库<b class='flag-5'>Assert</b><b class='flag-5'>断言</b>机制

    【C语言进阶】利用assert高效排查你的C程序

    【C语言进阶】利用assert高效排查你的C程序
    的头像 发表于 08-31 13:27 1750次阅读

    C语言断言函数assert()的应用,清晰明了!

    这样可以快速发现并定位软件问题,同时对系统错误进行自动报警。对于在系统中隐藏很深,用其他手段极难发现的问题也可以通过断言进行定位,从而缩短软件问题定位时间,提高系统的可测性。
    的头像 发表于 04-12 10:02 591次阅读

    防御式编程之断言assert的使用

    防御式编程的重点就是需要防御一些程序未曾预料的错误,这是一种提高软件质量的辅助性方法,断言assert就用于防御式编程,编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设。使用
    的头像 发表于 04-19 11:35 375次阅读