assert

Identifies program logic errors.

Library:LibC
Classification:ANSI
Service:General C Services

Syntax

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

Parameters

expression

(IN) Specifies an expression to evaluate.

Remarks

The assert macro takes no action if expression evaluates to TRUE (nonzero). If expression evaluates to zero, assert prints a diagnostic message to stderr and terminates the program.The diagnostic message has the following form:

     Assertion failed: expression, file filename, line 
     linenumber
  

The filename and linenumber variables are defined as follows:

filename

Specifies the name of the source file.

linenumber

Specifies the line number of the assertion that failed in the source file.

The filename and linenumber values are the values of the preprocessing macros __FILE__ and __LINE__, respectively.

The given expression should be chosen so that it is true when the program is functioning as intended. After the program has been debugged, the special “no debug” identifier NDEBUG can be used to remove assert functions from the program when it is recompiled. If NDEBUG is defined (with any value) with a -d command line option or by using a #define directive, the C preprocessor ignores all uses of the assert macro in the program source.

See Also

Example

  #include <assert.h>
  
  int two_div( int by )
  {
     assert(by != 0);// ensure we don’t divide by 0!
     return (2/by);
  }