Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C assert.h: the assert macro

Reference for assert.h: check assumptions with assert(), switch the checks off with NDEBUG, avoid side effects inside assertions, and use static_assert for compile time.

assert(condition) is a debugging aid. If the condition is true, nothing happens. If it is false, the program prints a diagnostic that names the failed expression, the source file, the line and the function, and then calls abort().

Use assertions to document and check things that must be true if your code is correct: preconditions of internal functions, invariants of data structures, "this can never happen" branches. They are not for errors that can legitimately occur at run time, such as a missing file or bad user input, because release builds can remove them entirely.

C
#include <assert.h>

assert(index < length);

Try it

C Checking assumptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C NDEBUG removes assertions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <assert.h>

void assert(scalar expression);       /* a macro */
static_assert(constant-expression, "message");   /* C23: keyword; C11: macro for _Static_assert */
Name Description
expression Any scalar (number or pointer) expression. A value of 0 or NULL means failure.

Returns: nothing. When the expression is false, assert writes a message to stderr and calls abort(). On glibc the message looks like program: main.c:7: average: Assertion `count > 0' failed. (program name, file, line, function, expression). The exact text differs between libraries, and the program ends with SIGABRT.

Errors / undefined behavior: the macro may be redefined by including <assert.h> again after changing NDEBUG; each include applies the current setting. Defining or undefining assert yourself is undefined.

Usage notes

  • NDEBUG removes the check. If the macro NDEBUG is defined when <assert.h> is included (usually by compiling with -DNDEBUG for release builds), assert(x) expands to nothing and x is not evaluated.
  • Never put side effects in an assertion. assert(fclose(f) == 0) or assert(++n < 10) changes the behavior of the release build. Evaluate into a variable first, then assert on the variable (and cast it to void if it would otherwise be unused).
  • Do not use it for input validation. Anything that depends on the outside world (files, network, users) needs a real check that stays in release builds.
  • It aborts, it does not return. There is no way to continue after a failed assertion, and stdout buffers are not flushed by abort(). Output printed before the failure may be missing.
  • static_assert checks at compile time. The condition must be an integer constant expression; a failure is a compile error showing your message. It is also available as a keyword in C23 (the message is optional there) and as a macro from this header in C11. See static_assert.
  • Messages. A well-known trick is assert(condition && "explanation"): a string literal is always true, but it shows up in the failure text.
  • C23 note. C23 changed assert to accept an expression containing commas that are not inside parentheses, such as a compound literal initializer. Older libraries (including some glibc versions) implement assert as taking one argument, so wrap such expressions in extra parentheses to stay portable.
  • Standard version: C89; static_assert since C11.