Skip to the content.

assertf.h

Try it online Codacy Badge License

assertf.h is a header-only formattable assert macros library, a possible alternative to the #include <assert.h>.

With enhanced assertions, we can debug/test code better.

Platform

Platform

assertf.h originally targets to embedded systems, it also can be used nearly all UNIX-like, including Windows systems.

Example

#include <stdio.h>
#include <unistd.h>
#include <errno.h>

#define ASSERTF_DEF_ONCE
#include "assertf.h"

int main(void)
{
    int e = rmdir("/tmp");
    assert_eqf(e, 0, %d, "rmdir(2) fail, errno: %d", errno);    /* Line 11 */
    // #define EACCES          13      /* Permission denied */
    return 0;
}

Sample assertion failure output(try it online)

Samplt output

Integration

In order to deploy assertf.h to your existing C/C++ project, you need:

  1. Download assertf.h to your source code directory

  2. In one of your C/C++ file(typically project main file), write:
     // Define ASSERTF_DEF_ONCE once and only once
     #define ASSERTF_DEF_ONCE
     #include "assertf.h"
    
  3. If other C/C++ files needs to use assertf.h, just type #include "assertf.h".

  4. If you want to disable assertf.h on release build, please specify -DASSERTF_DISABLE in Makefile, CMakeLists.txt, etc.

API

Nearly all assertf.h APIs prefixed with assert, the most basic API is the

Caveats

Pro tips

FAQ