Jump to content

assert.h

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Spitzak (talk | contribs) at 04:13, 17 March 2023 (Use: Put example output back in and removed some erroneous statements and bloat). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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. Additionally, assert.h is available in C++.

Use

The assert macro implements runtime assertion. If the expression within fails, the assert macro will print a message to stderr and call abort(). The message printed includes the source filename and the source line number[1] and (since C99) the name of the function the assert statement is in, and the expression itself.[2] In ANSI C, the expression in the assert macro must be a signed integer; in C99, the expression may be of any scalar type.[3] 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.[4]

An example program, and the output (using GCC on Linux):

#include <assert.h>
int main()
{
    int a = 1;
    assert(a > 6);
    return 0;
}
program: source.c:5: main: Assertion 'a > 6' failed.
Aborted (core dumpted)

The value 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),[2] so the assertion does not happen. Notice that this does not execute the expression, relying on side-effects of the assert expression is a common mistake.

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));.[5]

static_assert

The static_assert macro, added in C++11, serves a similar purpose to the assert macro. The static_assert macro 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 macro has no effect.[6] In C++17, this assertion failure message was made optional, and the subsequent message is omitted if not specified.[7] Alternatively, the <cassert> header from the assert.h header may also be used to declare the assert macro.[8] assert.h may also be included directly to import the assert macro. In both cases, the assert macro in C++ is functionally equivalent to its C counterpart.[9]

In C11, the _Static_assert declaration was added. Unlike the assert macro, _Static_assert is compile-time. As a result, _Static_assert results in a compilation error if its first argument evaluates to zero, or is false. 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.[11][12] Gnulib defines static_assert for platforms that do not use C11 and does not require assert.h to be included.[13]

Notes

References

Citations

  1. ^ Kernighan & Ritchie 1988, p. 253-254.
  2. ^ a b ISO/IEC JTC 1/SC 22/WG14 1999, p. 169.
  3. ^ "Linux Programmer's Manual". August 25, 2002. Retrieved March 14, 2023.
  4. ^ Reekie, John (December 7, 1995). "How to use assertions in C". University of California, Berkeley. Retrieved March 14, 2023.
  5. ^ Gregoire 2021, p. 1058.
  6. ^ ISO/IEC JTC 1/SC 22/WG21 2012, p. 134.
  7. ^ Swaminathan 2017, p. 13.
  8. ^ Lischner 2009, p. 375.
  9. ^ Binder 2000, p. 860.
  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.

Bibliography