Borland C++ reference

assert

raw OCR

Tests a condition and possibly aborts.

Defined in header <assert.h>

Syntax

#include <assert.h>
void assert(int test);

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

assert is a macro that expands to an if statement; if test evaluates to zero, assert prints a message on stderr and aborts the program (by calling abort).

assert prints this message:

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 effect is to comment out the assert statement.

Return value

None.

See also

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)
   additern(NULL);
   return 0;

Program   output

Assertion failed: itemptr  != NULL,
file C:\BC\ASSERT.C, line 12

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.