1 /*************************************************************************2 > File name:assert.c3 > Author:Mr.Yang4 > Purpose: Demonstrating the use of function assert5 > Created time:2017 May 29 Monday 19:57 54 seconds6 ************************************************************************/7 8#include <stdio.h>9 //#define NDEBUG Frequent calls can affect the performance of your program, and you can disable the Assert after debugging endsTen#include <assert.h>//use the Assert function to introduce package files One#include <stdlib.h> A - intMainvoid) - { theFILE *FP; - - /*open in a written way*/ -fp = fopen ("Test.txt","W"); + assert (FP); - fclose (FP); + A /*Open in read-only mode*/ atfp = fopen ("Newtest.txt","R");//Newtest.txt is not there. - assert (FP); -Fclose (FP);//The program will never be executed here. - - return 0; - }
Execution Result:
Assert:assert.c:24:main:assertion ' FP ' failed.
has been abandoned
Function Prototypes:
#include <assert.h>
void assert (int expression);
The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.
Reference Source: http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html
Usage of assert in C language