User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







cpp:cassert:assert

assert

    void assert(scalar expression);

With assert you can test your programm for logical errors.

The assert () macro will insert diagnostics into programs

When executed, if expression (which is a scalar type you have) is false (ie, compares equal to 0),
assert (), provide information about the particular call that failed to stderr and abort ()

cpp Sourcecode Example

#include <iostream> /* including standard library */
#include <cassert>
 
using namespace std;
 
int testfunc(int a, int b) {
   assert( (a >= 0 && a >= b) && (b >= 0) );
   return a / b;
}
 
int main ( void ) {
cout << "9 / 4 = " << testfunc(9, 4) << endl;
cout << "1 / 0 =  " << testfunc(1, 1) << endl;
cout << "1 / 0 = " << testfunc(0, 9) << endl;
return 0;
}

Output

  9 / 4 = 2
  1 / 0 = 1
  assert: assert.cpp:7: testfunc: Assertion `(a >= 0 && a >= b) && (b >= 0)' failed.
  canceled

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
cpp/cassert/assert.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz