C Programming/Code library

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: GObject Index Next: Reference Tables

The following is an implementation of the Standard C99 version of <assert.h>:

 /* assert.h header */
 #undef assert
 #ifdef NDEBUG
 #define assert(_Ignore) ((void)0)
 #else
 void _Assertfail(char *, char *, int, char *);
 #define assert(_Test) ((_Test)?((void)0):_Assertfail(#_Test,__FILE__,__LINE__,__func__))
 #endif
 /* END OF FILE */
 /* xassertfail.c -- _Assertfail function */
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
 void
 _Assertfail(char *test, char *filename, int line_number, char *function_name)
 {
    fprintf(stderr, "Assertion failed: %s, function %s, file %s, line %d.",
            test, function_name, filename, line_number);
    abort();
 }
 /* END OF FILE */
Previous: Mixing languages Index Next: Reference Tables