在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

    文章

    7628

    瀏覽量

    139708
  • 程序
    +關注

    關注

    117

    文章

    3817

    瀏覽量

    82170
  • 代碼
    +關注

    關注

    30

    文章

    4880

    瀏覽量

    70004
收藏 人收藏

    評論

    相關推薦

    C語言assert的使用

    assert意思是斷言,常用在程序的DEBUG版本中。
    發表于 07-21 14:51 ?1059次閱讀

    什么是斷言C語言中斷言的語法和用法

    在軟件開發過程中,我們經常需要處理各種錯誤和異常情況。為了提高代碼的健壯性和可靠性,我們需要使用一些工具和技術來檢測和處理這些問題。本篇博客將深入探討C語言中斷言的使用,幫助讀者更好地理解和應用斷言,提高代碼的質量和可維護性。
    發表于 08-03 10:34 ?3297次閱讀

    解析C語言斷言函數的使用

    對于斷言,相信大家都不陌生,大多數編程語言也都有斷言這一特性。簡單地講,斷言就是對某種假設條件進行檢查。 在 C
    發表于 08-08 09:51 ?610次閱讀
    解析<b class='flag-5'>C</b><b class='flag-5'>語言</b><b class='flag-5'>斷言</b>函數的使用

    請問HAL函數對Handle有效性的檢查為什么不是用assert_param斷言

    )); ...... } 以HAL_SPI_Init為例,hspi參數的檢查并沒有使用assert_param斷言宏,如果是我實現的話,我會用assert_param(hspi != NULL)實現。一般
    發表于 05-08 07:00

    斷言ASSERT)的用法

    STM32中經常出現assert函數,網上看了篇博客分享下:我一直以為assert僅僅是個報錯函數,事實上,它居然是個宏,并且作用并非“報錯”。  在經過對其進行一定了解之后,對其作用及用法有了一定
    發表于 08-23 09:33

    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

    C語言簡單概述

    C語言簡介C語言簡介C
    發表于 11-20 14:14 ?0次下載

    怎么理解Assert中的斷言語句?

    為什么項目中的代碼需要有Assert斷言語句?
    的頭像 發表于 03-03 14:12 ?2928次閱讀

    如何得當使用C語言的特殊的用法

    C語言有很多特殊的用法,如果這些特殊用法使用得當,會是你的代碼變得更加有健壯,更加容易維護。 比如我們在使用STM32庫的斷言assert),你會發現官方提供了包含__FILE__
    的頭像 發表于 09-27 10:41 ?2084次閱讀
    如何得當使用<b class='flag-5'>C</b><b class='flag-5'>語言</b>的特殊的用法

    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 ?2371次閱讀

    C語言斷言函數assert()的應用,清晰明了!

    這樣可以快速發現并定位軟件問題,同時對系統錯誤進行自動報警。對于在系統中隱藏很深,用其他手段極難發現的問題也可以通過斷言進行定位,從而縮短軟件問題定位時間,提高系統的可測性。
    的頭像 發表于 04-12 10:02 ?1335次閱讀

    防御式編程之斷言assert的使用

    防御式編程的重點就是需要防御一些程序未曾預料的錯誤,這是一種提高軟件質量的輔助性方法,斷言assert就用于防御式編程,編寫代碼時,我們總是會做出一些假設,斷言就是用于在代碼中捕捉這些假設。使用
    的頭像 發表于 04-19 11:35 ?833次閱讀
    主站蜘蛛池模板: 亚洲成a人不卡在线观看 | 色天天综合色天天看 | 日夜操在线视频 | 特黄特级高清免费视频毛片 | 亚洲欧美日韩色图 | 亚洲羞羞裸色私人影院 | 91成人在线免费视频 | 在线色综合| 午夜激情婷婷 | 午夜看片在线观看 | 亚洲综合欧美日本另类激情 | 久久久久久国产精品免费 | 亚洲国产香蕉视频欧美 | 97狠狠操 | 色噜噜噜噜 | 日本人xxxxxxxx6969 | 色婷婷激情综合 | 51精品国产 | 亚洲欧美人成网站综合在线 | 成 黄 色 激 情视频网站 | 人人艹人人干 | 欧美黑粗硬 | 黄色欧美网站 | 手机在线观看免费视频 | 九九热视频免费在线观看 | 国产亚洲自在精品久久 | 人人乳乳香蕉大免费 | 天天性视频 | 五月.com | 中文字幕二区三区 | 亚洲三级网址 | 丁香婷婷久久大综合 | h网站在线免费观看 | 中国xxxxx高清免费看视频 | 久久伊人男人的天堂网站 | 狠狠干伊人网 | 免费国内精品久久久久影院 | 中文日产国产精品久久 | 国产亚洲综合色就色 | 人人艹在线视频 | 高清视频 一区二区三区四区 |