一、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
發(fā)布評論請先 登錄
相關(guān)推薦
熱點推薦
什么是斷言?C語言中斷言的語法和用法
在軟件開發(fā)過程中,我們經(jīng)常需要處理各種錯誤和異常情況。為了提高代碼的健壯性和可靠性,我們需要使用一些工具和技術(shù)來檢測和處理這些問題。本篇博客將深入探討C語言中斷言的使用,幫助讀者更好地理解和應用斷言,提高代碼的質(zhì)量和可維護性。
發(fā)表于 08-03 10:34
?3406次閱讀
解析C語言斷言函數(shù)的使用
對于斷言,相信大家都不陌生,大多數(shù)編程語言也都有斷言這一特性。簡單地講,斷言就是對某種假設條件進行檢查。 在 C
發(fā)表于 08-08 09:51
?644次閱讀

請問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
何為斷言?斷言該怎么使用呢
存在錯誤。因此,斷言是提高程序可靠性的有效手段。也是開發(fā)階段快速定位問題的一種很好防御式編程方法。在C語言中,斷言是一些條件判斷的宏。比如C
發(fā)表于 09-21 14:59
如何得當使用C語言的特殊的用法
C語言有很多特殊的用法,如果這些特殊用法使用得當,會是你的代碼變得更加有健壯,更加容易維護。 比如我們在使用STM32庫的斷言(assert),你會發(fā)現(xiàn)官方提供了包含__FILE__

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

C語言斷言函數(shù)assert()的應用,清晰明了!
這樣可以快速發(fā)現(xiàn)并定位軟件問題,同時對系統(tǒng)錯誤進行自動報警。對于在系統(tǒng)中隱藏很深,用其他手段極難發(fā)現(xiàn)的問題也可以通過斷言進行定位,從而縮短軟件問題定位時間,提高系統(tǒng)的可測性。
防御式編程之斷言assert的使用
防御式編程的重點就是需要防御一些程序未曾預料的錯誤,這是一種提高軟件質(zhì)量的輔助性方法,斷言assert就用于防御式編程,編寫代碼時,我們總是會做出一些假設,斷言就是用于在代碼中捕捉這些假設。使用
評論