The standard header file assert.h can be included to obtain access to the assert( ) macro . This macro can be used to encure that the value of an expression is what you expect it to be . Suppose that you are writing a critical function and that you want to be sure the arguments satisfy certain conditions.

eg:
   void fun(int n){
      .....
      assert(n>0);
      .....
  }

If the assertion fails , then the system will print out a message and abort the program.
o/p -  Assertion failed: .....
          Abnormal program termination.
Hence it is used in error handling.

Assertions are easy to write, and help other readers of the code understand its intent.

The use of assertions is considerd good programming methodology.

Categories:

Leave a Reply