一、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語言中斷言的語法和用法
在軟件開發過程中,我們經常需要處理各種錯誤和異常情況。為了提高代碼的健壯性和可靠性,我們需要使用一些工具和技術來檢測和處理這些問題。本篇博客將深入探討C語言中斷言的使用,幫助讀者更好地理解和應用斷言,提高代碼的質量和可維護性。
發表于 08-03 10:34
?3297次閱讀
請問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語言中,斷言是一些條件判斷的宏。比如C
發表于 09-21 14:59
如何得當使用C語言的特殊的用法
C語言有很多特殊的用法,如果這些特殊用法使用得當,會是你的代碼變得更加有健壯,更加容易維護。 比如我們在使用STM32庫的斷言(assert),你會發現官方提供了包含__FILE__

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

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