C語言中assert()斷言函數的概念及用法


  斷言函數的格式如下所示:

  void assert (int expression);
如果參數expression等於零,一個錯誤消息將會寫入到設備的標准錯誤集並且會調用abort函數,就會結束程序的執行。
  斷言的消息會顯示庫依賴,但是它也包含一下信息,源文件的名字,處於哪一行,在哪兒發生的,一般的格式如下:
Assertion failed: expression, file filename, line line number
該函數的頭文件如下所示:
  <assert.h>

  該函數的源碼應用如下所示:

 1 /* assert example */
 2 #include <stdio.h>      /* printf */
 3 #include <assert.h>     /* assert */
 4 
 5 void print_number(int* myInt) {
 6   assert (myInt!=NULL);
 7   printf ("%d\n",*myInt);
 8 }
 9 
10 int main ()
11 {
12   int a=10;
13   int * b = NULL;
14   int * c = NULL;
15 
16   b=&a;
17 
18   print_number (b);
19   print_number (c);
20 
21   return 0;
22 }

 參考文檔:

1 http://www.cplusplus.com/reference/cassert/assert/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM