assert.h
C standard library (libc) |
---|
General topics |
Miscellaneous headers |
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 the abort
function.[note 1] The message printed includes the source filename and the source line number, defined as the string literal __FILE__
and decimal constant __LINE__
, respectively.[1] Since C99, the message additionally includes the source function, of the identifier __func__
, 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, otherwise known as a null pointer, and to ensure that indexes and size values are not negative and below a specified limit.[4]
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:[2]
#define assert(ignore) ((void)0)
The assert
macro does not include an error message. However, a custom message can be provided using the comma operator, as seen below.[5]
assert(("Orwellian statement", 2 + 2 == 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]
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++.[9] In C23, _Static_assert
was renamed to static_assert
and the string literal argument was made optional.[10][11] Gnulib defines static_assert
for platforms that do not use C11 and does not require assert.h to be included.[12]
Notes
- ^ As defined in
stdlib.h
References
Citations
- ^ Kernighan & Ritchie 1988, p. 253-254.
- ^ a b ISO/IEC JTC 1/SC 22/WG14 1999, p. 169.
- ^ "Linux Programmer's Manual". August 25, 2002. Retrieved March 14, 2023.
- ^ Reekie, John (December 7, 1995). "How to use assertions in C". University of California, Berkeley. Retrieved March 14, 2023.
- ^ Gregoire 2021, p. 1058.
- ^ ISO/IEC JTC 1/SC 22/WG21 2011, p. 134.
- ^ Swaminathan 2017, p. 13.
- ^ Lischner 2009, p. 375.
- ^ Prata 2013, p. 762-763.
- ^ Gustedt 2022, p. 3.
- ^ Ballman & Grammatech 2018, p. 1.
- ^ "GNU Gnulib". Free Software Foundation. February 6, 2023. Retrieved March 14, 2023.
Bibliography
- Ballman, Aaron; Grammatech (July 6, 2018). Harmonizing static_assert with C++ (Report).
- Gregoire, Marc (2021). Professional C++ (5th ed.). Hoboken: Wiley. ISBN 9781119695455.
- Gustedt, Jens (February 15, 2022). Revise spelling of keywords (Report).
- Kernighan, Brian; Ritchie, Dennis (1988). The C Programming Language (2nd ed.). Hoboken: Prentice Hall. ISBN 9780131103627.
- Lischner, Ray (2009). C++ In a Nutshell: A Desktop Quick Reference (2nd ed.). Sebastopol: O'Reilly Media. ISBN 9781449378837.
- ISO/IEC JTC 1/SC 22/WG14 (December 1999). ISO/IEC 9899:1999 (Report).
{{cite report}}
: CS1 maint: numeric names: authors list (link) - ISO/IEC JTC 1/SC 22/WG21 (January 2012). ISO/IEC 14882:2011 (Report).
{{cite report}}
: CS1 maint: numeric names: authors list (link) - Prata, Stephen (2013). C Primer Plus (6th ed.). London: Pearson Education. ISBN 9780133432381.
- Swaminathan, Jeganathan (2017). Mastering C++ Programming. Birmingham: Packt. ISBN 9781786461629.