assert

Used for implementing run-time diagnostics in programs.

Format

#include  <assert.h>

void assert  (int expression);

Argument

expression
An expression that has an int type.

Description

When assert is executed, if expression is false (that is, it evaluates to 0), assert writes information about the particular call that failed (including the text of the argument, the name of the source file, and the source line number; the latter two are respectively the values of the preprocessing macros __FILE__ and __LINE__) to the standard error file in an implementation- defined format. Then, it calls the abort function.

The assert function writes a message in the following form:

Assertion failed:  expression, file aaa, line nnn

If expression is true (that is, it evaluates to nonzero) or if the signal SIGABRT is being ignored, assert returns no value.


Note
If a null character ('\0') is part of the expression being asserted, then only the text up to and including the null character is printed, since the null character effectively terminates the string being output.

Compiling with the CC command qualifier /DEFINE=NDEBUG or with the preprocessor directive #define NDEBUG ahead of the #include assert statement causes the assert function to have no effect.

Example

    #include <stdio.h>
    #include <assert.h>
    
    main()
    {
       printf("Only this and the assert\n");
       assert( 1==2 );    /* expression is FALSE */
    
       /* abort should be called so the printf will not happen. */
    
       printf("FAIL abort did not execute");
    }
    


Previous Page | Next Page | Table of Contents | Index