女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

C語言assert(斷言)簡介

CHANBAEK ? 來源: 嵌入式學習和實踐 ? 作者: 嵌入式學習和實踐 ? 2023-11-17 16:33 ? 次閱讀

一、assert(斷言)簡介

assert的功能,條件為真,程序繼續(xù)執(zhí)行;如果斷言為假(false),則程序終止。

assert是個 宏定義

頭文件:

#include < assert.h >

原型:

void assert(scalar expression);

返回值:無返回值。

頭文件assert.h內(nèi)容如下:

/* 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為真,
執(zhí)行 __ASSERT_VOID_CAST (0)  ;
否則執(zhí)行 __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))

條件表達式 ,偽代碼:

a?b:c
//如果a為真,執(zhí)行b;
//如果a為假,執(zhí)行c

二、測試代碼

參數(shù)數(shù)量為2,則輸出參數(shù)。否則輸出錯誤信息,并終止程序執(zhí)行。測試代碼如下:

#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;
}

圖片

圖片

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • C語言
    +關(guān)注

    關(guān)注

    180

    文章

    7630

    瀏覽量

    140450
  • 程序
    +關(guān)注

    關(guān)注

    117

    文章

    3824

    瀏覽量

    82438
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4887

    瀏覽量

    70266
收藏 人收藏

    評論

    相關(guān)推薦
    熱點推薦

    C語言assert的使用

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

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

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

    解析C語言斷言函數(shù)的使用

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

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

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

    斷言ASSERT)的用法

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

    C語言中斷言如何去使用

    文章目錄1 C語言中斷言的使用1.1 處理方式1.2 原型定義1.3 示例代碼1 C語言中斷言的使用1.1 處理方式如果斷言的條件返回錯誤,
    發(fā)表于 07-14 08:15

    C語言中斷言是怎樣使用的?

    C語言中斷言是怎樣使用的?
    發(fā)表于 10-14 07:18

    何為斷言斷言該怎么使用呢

    存在錯誤。因此,斷言是提高程序可靠性的有效手段。也是開發(fā)階段快速定位問題的一種很好防御式編程方法。在C語言中,斷言是一些條件判斷的宏。比如C
    發(fā)表于 09-21 14:59

    C語言簡單概述

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

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

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

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

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

    STM32函數(shù)庫Assert斷言機制

    編寫代碼時,我們總是會做出一些假設,斷言就是用于在代碼中捕捉這些假設,可以將斷言看作是異常處理的一種高級形式。斷言表示為一些布爾表達式,程序員相信在程序中的某個特定點該表達式值為真。可以在任
    發(fā)表于 02-08 15:29 ?2次下載
    STM32函數(shù)庫<b class='flag-5'>Assert</b><b class='flag-5'>斷言</b>機制

    C語言進階】利用assert高效排查你的C程序

    C語言進階】利用assert高效排查你的C程序
    的頭像 發(fā)表于 08-31 13:27 ?2484次閱讀

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

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

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

    防御式編程的重點就是需要防御一些程序未曾預料的錯誤,這是一種提高軟件質(zhì)量的輔助性方法,斷言assert就用于防御式編程,編寫代碼時,我們總是會做出一些假設,斷言就是用于在代碼中捕捉這些假設。使用
    的頭像 發(fā)表于 04-19 11:35 ?883次閱讀