assert.h

{{short description|Header file for C programs}}

{{lowercase title|title=assert.h}}

{{C Standard Library}}

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.

assert.h is defined in ANSI C as part of the C standard library. In the C++ programming language, assert.h and {{C++|}} 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 stderr and call abort(), defined in 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.{{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}} 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.{{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}}

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:

  1. include

int main()

{

void* pointer = 0;

assert(pointer);

return 0;

}

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

program: source.c:5: main: Assertion 'pointer' failed.

Aborted (core dumped)

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}}

=static_assert=

The {{code|static_assert}} macro, 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}} 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.{{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. 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 {{tt|assert.h}} to be included.{{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}}

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