{{short description|Header file for C programs}} {{lowercase title|title=assert.h}} {{C Standard Library}} '''{{mono|<assert.h>}}''' is a header file in the C standard library. It defines the C preprocessor macro {{C-lang|assert}} and implements runtime assertion in C.
{{code|<assert.h>}} is defined in ANSI C as part of the C standard library. In the C++ programming language, {{code|<assert.h>}} and {{code|<cassert>}} are available; both are functionally equivalent.{{sfn|Binder|2000|p=860}}
==Use== The {{C-lang|assert}} macro implements runtime assertion. If the expression within it is false, the macro will print a message to <code>stderr</code> and call <code>abort()</code>, defined in {{mono|<stdlib.h>}}. The message includes the source filename and the source line number from the macros {{code|__FILE__}} and {{code|__LINE__}}, respectively.{{sfn|Kernighan|Ritchie|1988|p=253-254}} Since C99, the name of the function the assert statement is included as ({{code|__FUNC__}}) and the expression itself.{{sfn|ISO/IEC JTC 1/SC 22/WG14|1999|p=169}} In ANSI C, the expression in the {{C-lang|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 {{C-lang|assert}} macro explicitly allows any scalar type.<ref>{{Cite web |url=https://fog.misty.com/perry/osp/C99/man/assert.html |title=Linux Programmer's Manual |date=August 25, 2002 |access-date=March 14, 2023}}</ref> Two common uses of the {{C-lang|assert}} macro are to assert that a pointer is not null and to ensure that an array index is in-bounds.<ref>{{Cite web |url=https://ptolemy.berkeley.edu/~johnr/tutorials/assertions.html |title=How to use assertions in C |date=December 7, 1995 |last=Reekie |first=John |publisher=University of California, Berkeley |access-date=March 14, 2023}}</ref>
Below is a program using the {{C-lang|assert}} macro. This program will always evaluate {{C-lang|pointer}} as false, as {{C-lang|pointer}} is a null pointer and does not point to a valid memory location: <syntaxhighlight lang="c"> #include <assert.h> #include <stddef.h>
int main() { void* ptr = NULL; assert(ptr); return 0; } </syntaxhighlight>
Upon compiling the program and running it, a message similar to the following will be output: <syntaxhighlight lang="output"> program: source.c:5: main: Assertion 'ptr' failed. Aborted (core dumped) </syntaxhighlight> The definition of the {{C-lang|assert}} macro changes depending on the definition of another macro, {{C-lang|NDEBUG}}. If {{C-lang|NDEBUG}} is defined as a macro name, the {{C-lang|assert}} macro is defined as {{C-lang|#define assert(ignore) ((void)0)}},{{sfn|ISO/IEC JTC 1/SC 22/WG14|1999|p=169}} thus resulting in the macro not evaluating the expression. The use of {{C-lang|NDEBUG}} may affect the overall behavior of a program if one or more {{C-lang|assert}} statements contain side effects, as these statements are not evaluated.{{sfn|American National Standards Institute|1990|p=76}}
The {{C-lang|assert}} macro does not include an error message. However the comma operator can be used to add it to the printed expression, as in {{C-lang|1=assert(("Not Orwellian", 2 + 2 == 5));}}.{{sfn|Gregoire|2021|p=1058}}
==={{mono|static_assert}}=== The {{code|static_assert}} keyword, added in C++11, serves a similar purpose to the {{code|assert}} macro. Unlike the {{code|assert}} macro, {{code|static_assert}} runs at compile-time rather than at runtime.{{sfn|ISO/IEC JTC 1/SC 22/WG21|2012|p=134}} The original implementation used template hacks.{{Citation needed|date=March 2023}} The {{C++|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.{{sfn|ISO/IEC JTC 1/SC 22/WG21|2012|p=134}} In C++17, this assertion failure message was made optional, and the subsequent message is omitted if not specified.{{sfn|Swaminathan|2017|p=13}}
In C11, the functionally equivalent declaration {{code|_Static_assert}} was added. {{code|<assert.h>}} defines {{code|static_assert}} as an alias for {{code|_Static_assert}} to ensure parity with C++.{{sfn|Prata|2013|p=762-763}} In C23, {{code|_Static_assert}} was renamed to {{code|static_assert}} and the string literal argument was made optional.{{sfn|Gustedt|2022|p=3}}{{sfn|Ballman|Grammatech|2018|p=1}} Gnulib defines {{code|static_assert}} for platforms that do not use C11 and does not require {{code|<assert.h>}} to be included.<ref>{{Cite web |url=https://www.gnu.org/software//gnulib/manual/html_node/static_005fassert.html |title=GNU Gnulib |date=February 6, 2023 |publisher=Free Software Foundation |access-date=March 14, 2023}}</ref>
==={{mono|contract_assert}}=== The {{code|contract_assert}} keyword, added in C++26, is for contract assertions and used to verify internal conditions similar to the <code>assert()</code> macro by ensuring that a condition holds during execution.<ref>{{cite web|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2900r14.pdf|publisher=WG 22|website=open-std.org|author=Joshua Berne, Timur Doumler, Andrzej Krzemieński|title=Contracts for C++|date=13 February 2025}}</ref><ref>{{Cite web|title=Contract assertions (since C++26)|url=https://en.cppreference.com/w/cpp/language/contracts.html|website=cppreference.com|publisher=cppreference|access-date=9 November 2025}}</ref> <syntaxhighlight lang="cpp"> 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]; } </syntaxhighlight>
== Other languages == In Java, <code>assert</code> is a keyword.
In C#, there is no assertion macro or keyword, but instead classes <code>System.Diagnostics.Debug</code> and <code>System.Diagnostics.Trace</code> which provide <code>Assert()</code> methods.
In Rust, there is an <code>assert!()</code> macro.
== References == ===Citations=== {{Reflist}}
===Bibliography=== * {{Cite book |author=American National Standards Institute |date=1990 |title=Rationale for the ANSI C Programming Language |location=Summit |publisher=Silicon Press |isbn=9780929306070}} * {{Cite report |last1=Ballman |first1=Aaron |author2=Grammatech |date=July 6, 2018 |title=Harmonizing static_assert with C++}} * {{Cite book |last=Binder |first=Robert |date=2000 |title=Testing Object-oriented Systems: Models, Patterns, and Tools |edition=2nd |location=Boston |publisher=Addison-Wesley |isbn=9780201809381}} * {{Cite book |last=Gregoire |first=Marc |date=2021 |title=Professional C++ |edition=5th |location=Hoboken |publisher=Wiley |isbn=9781119695455}} * {{Cite report |last=Gustedt |first=Jens |date=February 15, 2022 |title=Revise spelling of keywords}} * {{Cite book |last1=Kernighan |first1=Brian |author-link1=Brian Kernighan |last2=Ritchie |first2=Dennis |author-link2=Dennis Ritchie |date=1988 |title=The C Programming Language |edition=2nd |location=Hoboken |publisher=Prentice Hall |isbn=9780131103627}} * {{Cite book |last=Lischner |first=Ray |date=2009 |title=C++ In a Nutshell: A Desktop Quick Reference |edition=2nd |location=Sebastopol |publisher=O'Reilly Media |isbn=9781449378837}} * {{Cite report |author=ISO/IEC JTC 1/SC 22/WG14 |date=December 1999 |title=ISO/IEC 9899:1999}} * {{Cite report |author=ISO/IEC JTC 1/SC 22/WG21 |date=January 2012 |title=ISO/IEC 14882:2011}} * {{Cite book |last=Prata |first=Stephen |date=2013 |title=C Primer Plus |edition=6th |location=London |publisher=Pearson Education |isbn=9780133432381}} * {{Cite book |last=Swaminathan |first=Jeganathan |date=2017 |title=Mastering C++ Programming |location=Birmingham |publisher=Packt |isbn=9781786461629}}
Category:C standard library headers