Using the assert( ) Macro - C++ Preprocessor

C++ examples for Preprocessor:Macro

Introduction

The assert() preprocessor macro is defined in the library header cassert.

This enables you to test logical expressions in your program.

Demo Code

#include <iostream>
#include <cassert>
int main()//from   w  w w. j  a  v  a2 s  .c  o  m
{
  int y {5};

  for(int x {} ; x < 20 ; ++x)
  {
    std::cout << "x = " << x << " y = " << y << std::endl;
    assert(x<y);
  }
}

Result

Switching Off assert() Macros

You can switch off the preprocessor assertion mechanism when you recompile the program by defining NDEBUG:

#define NDEBUG

assert() is for detecting programming errors, not for handling errors at runtime.


Related Tutorials