Article · Wikipedia archive · Last revised Jul 13, 2026

Assert.h

<assert.h> is a header file in the C standard library. It defines the C preprocessor macro assert and implements runtime assertion in C.

Last revised
Jul 13, 2026
Read time
≈ 4 min
Length
871 w
Citations
17
Source

<assert.h> is a header file in the C standard library. It defines the C preprocessor macro assert and implements runtime assertion in C.

<assert.h> is defined in ANSI C as part of the C standard library. In the C++ programming language, <assert.h> and <cassert> are available; both are functionally equivalent.1

Use

The assert macro implements runtime assertion. If the expression within it is false, the macro will print a message to stderr and call abort(), defined in <stdlib.h>. The message includes the source filename and the source line number from the macros __FILE__ and __LINE__, respectively.2 Since C99, the name of the function the assert statement is included as (__FUNC__) and the expression itself.3 In ANSI C, the expression in the assert macro is defined as signed integer, although any expression that can be implicitly cast to a signed integer may be used. In C99, the assert macro explicitly allows any scalar type.4 Two common uses of the assert macro are to assert that a pointer is not null and to ensure that an array index is in-bounds.5

Below is a program using the assert macro. This program will always evaluate pointer as false, as pointer is a null pointer and does not point to a valid memory location:

#include <assert.h>
#include <stddef.h>

int main() {
    void* ptr = NULL;
    assert(ptr);
    return 0;
}

Upon compiling the program and running it, a message similar to the following will be output:

program: source.c:5: main: Assertion 'ptr' failed.
Aborted (core dumped)

The definition of the assert macro changes depending on the definition of another macro, NDEBUG. If NDEBUG is defined as a macro name, the assert macro is defined as #define assert(ignore) ((void)0),3 thus resulting in the macro not evaluating the expression. The use of NDEBUG may affect the overall behavior of a program if one or more assert statements contain side effects, as these statements are not evaluated.6

The assert macro does not include an error message. However the comma operator can be used to add it to the printed expression, as in assert(("Not Orwellian", 2 + 2 == 5));.7

static_assert

The static_assert keyword, added in C++11, serves a similar purpose to the assert macro. Unlike the assert macro, static_assert runs at compile-time rather than at runtime.8 The original implementation used template hacks. The static_assert keyword takes in a constant expression that can be converted into a Boolean and a string literal; if the expression fails, the string literal is returned, otherwise, the assertion has no effect.8 In C++17, this assertion failure message was made optional, and the subsequent message is omitted if not specified.9

In C11, the functionally equivalent declaration _Static_assert was added. <assert.h> defines static_assert as an alias for _Static_assert to ensure parity with C++.10 In C23, _Static_assert was renamed to static_assert and the string literal argument was made optional.1112 Gnulib defines static_assert for platforms that do not use C11 and does not require <assert.h> to be included.13

contract_assert

The contract_assert keyword, added in C++26, is for contract assertions and used to verify internal conditions similar to the assert() macro by ensuring that a condition holds during execution.1415

int f(vector<int>& v)
    pre (v.size() >= 1 && v[0] > 0)
    post (r: r == v[0] && r != 1) {
    // ...
    contract_assert(v[0] != 1);
    // ...
    return v[0];
}

Other languages

In Java, assert is a keyword.

In C#, there is no assertion macro or keyword, but instead classes System.Diagnostics.Debug and System.Diagnostics.Trace which provide Assert() methods.

In Rust, there is an assert!() macro.

References

References

Citations

  1. Binder 2000, p. 860.
  2. Kernighan & Ritchie 1988, p. 253-254.
  3. ISO/IEC JTC 1/SC 22/WG14 1999, p. 169.
  4. "Linux Programmer's Manual". August 25, 2002. Retrieved March 14, 2023.
  5. Reekie, John (December 7, 1995). "How to use assertions in C". University of California, Berkeley. Retrieved March 14, 2023.
  6. American National Standards Institute 1990, p. 76.
  7. Gregoire 2021, p. 1058.
  8. ISO/IEC JTC 1/SC 22/WG21 2012, p. 134.
  9. Swaminathan 2017, p. 13.
  10. Prata 2013, p. 762-763.
  11. Gustedt 2022, p. 3.
  12. Ballman & Grammatech 2018, p. 1.
  13. "GNU Gnulib". Free Software Foundation. February 6, 2023. Retrieved March 14, 2023.
  14. Joshua Berne, Timur Doumler, Andrzej Krzemieński (13 February 2025). "Contracts for C++" (PDF). open-std.org. WG 22.{{cite web}}: CS1 maint: multiple names: authors list (link)
  15. "Contract assertions (since C++26)". cppreference.com. cppreference. Retrieved 9 November 2025.

Bibliography