User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:assert.h:assert

Table of Contents

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 ()

C Sourcecode Example

#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
#include <assert.h>
 
int testfunc(int a, int b) {
   assert( (a >= 0 && a >= b) && (b >= 0) );
   return a / b;
}
 
int main ( void ) {
printf("9 / 4 = %d\n", testfunc(9, 4) );
printf("1 / 1 = %d\n", testfunc(1, 1) );
printf("0 / 9 = %d\n", testfunc(0, 9) );
return 0;
}

Output

  user@host:~$ ./assert 
  9 / 4 = 2
  1 / 1 = 1
  assert: assert.c:6: 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
c/assert.h/assert.txt · Last modified: 2024/02/16 01:06 (external edit)

Impressum Datenschutz