assert

From RAD Studio
Jump to: navigation, search

Go Up to assert.h Index


Header File

assert.h

Prototype

void assert(int test);

Description

Tests a condition and possibly aborts.

assert is a macro that expands to an if statement; if test evaluates to zero, the assert macro calls the _assert function

void _RTLENTRY _EXPFUNC _assert(char * __cond, char * __file, int __line);

and aborts the program. The _assert function calls abort and asserts the following a message on stderr:

Assertion failed: test, file filename, line linenum

The filename and linenum listed in the message are the source file name and line number where the assert macro appears.

If you place the #define NDEBUG directive ("no debugging") in the source code before the #include <assert.h> directive, the macro expands to a no-op, the effect is to comment out the assert statement.

Return Value

None.

Portability

POSIX ANSI C ANSI C++ Win32 Win64 OS X
assert + + + +

Example

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct ITEM {
    int key;
    int value;
};

/* add item to list, make sure list is not  null */
void additem(struct ITEM *itemptr) {
    assert(itemptr != NULL);
    /* add item to list */
}

int main(void) {
    additem(NULL);
    return 0;
}