/*********************************************************************
* Function: Test assert
* Author : Samson
* Date : 12/12/2011
* Test platform:
* GNU Linux version 2.6.29.4
* gcc version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC)
* *******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
int
main(int argc, char *argv[])
{
assert(argc != 2);
printf("argc is %d\n", argc);
exit(0);
}
assert當在條件為false的時候程式會被中斷,它的用處在開發人員進行調試時非常有用,能夠準確的定位到出錯的檔案:函數:行號。當不加NDEBUG宏時,此測試程式的輸出為:
a.out: testassert.c:9: main: Assertion `argc != 2' failed.
已放棄
但在進行程式發行版本的時候,使用者不希望這樣突然直接的給中斷了,對使用者要"柔和",柔和的話就使用有提示的if語句或列印等。
柔和版本的編譯方式為:gcc testassert.c -DNDEBUG
run result:
[root@UFO testc]# ./a.out 2
argc is 2
直言直去版本編譯方式為:gcc testassert.c
run result:
[root@UFO testc]# ./a.out 2
a.out: testassert.c:9: main: Assertion `argc != 2' failed.
已放棄