SUPPORT THE WORK

GetWiki

assert.h

ARTICLE SUBJECTS
aesthetics  →
being  →
complexity  →
database  →
enterprise  →
ethics  →
fiction  →
history  →
internet  →
knowledge  →
language  →
licensing  →
linux  →
logic  →
method  →
news  →
perception  →
philosophy  →
policy  →
purpose  →
religion  →
science  →
sociology  →
software  →
truth  →
unix  →
wiki  →
ARTICLE TYPES
essay  →
feed  →
help  →
system  →
wiki  →
ARTICLE ORIGINS
critical  →
discussion  →
forked  →
imported  →
original  →
assert.h
[ temporary import ]
please note:
- the content below is remote from Wikipedia
- it has been imported raw for GetWiki
{{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.WEB,fog.misty.com/perry/osp/C99/man/assert.html, Linux Programmer’s Manual, August 25, 2002, 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.WEB,ptolemy.berkeley.edu/~johnr/tutorials/assertions.html, How to use assertions in C, December 7, 1995, Reekie, John, University of California, Berkeley, 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.WEB,www.gnu.org/software//gnulib/manual/html_node/static_005fassert.html, GNU Gnulib, February 6, 2023, Free Software Foundation, March 14, 2023,

References

Citations

{{Reflist}}

Bibliography

  • BOOK, American National Standards Institute, 1990, Rationale for the ANSI C Programming Language, Summit, Silicon Press, 9780929306070,
  • REPORT, Ballman, Aaron, Grammatech, July 6, 2018, Harmonizing static_assert with C++,
  • BOOK, Binder, Robert, 2000, Testing Object-oriented Systems: Models, Patterns, and Tools, 2nd, Boston, Addison-Wesley, 9780201809381,
  • BOOK, Gregoire, Marc, 2021, Professional C++, 5th, Hoboken, Wiley (publisher), Wiley, 9781119695455,
  • REPORT, Gustedt, Jens, February 15, 2022, Revise spelling of keywords,
  • BOOK, Kernighan, Brian, Brian Kernighan, Ritchie, Dennis, Dennis Ritchie, 1988, The C Programming Language, 2nd, Hoboken, Prentice Hall, 9780131103627,
  • BOOK, Lischner, Ray, 2009, C++ In a Nutshell: A Desktop Quick Reference, 2nd, Sebastopol, O’Reilly Media, 9781449378837,
  • REPORT, ISO/IEC JTC 1/SC 22, ISO/IEC JTC 1/SC 22/WG14, December 1999, ISO/IEC 9899:1999,
  • REPORT, ISO/IEC JTC 1/SC 22, ISO/IEC JTC 1/SC 22/WG21, January 2012, ISO/IEC 14882:2011,
  • BOOK, Prata, Stephen, 2013, C Primer Plus, 6th, London, Pearson Education, 9780133432381,
  • BOOK, Swaminathan, Jeganathan, 2017, Mastering C++ Programming, Birmingham, Packt, 9781786461629,


- content above as imported from Wikipedia
- "assert.h" does not exist on GetWiki (yet)
- time: 10:06am EDT - Wed, May 22 2024
[ this remote article is provided by Wikipedia ]
LATEST EDITS [ see all ]
GETWIKI 21 MAY 2024
GETWIKI 09 JUL 2019
Eastern Philosophy
History of Philosophy
GETWIKI 09 MAY 2016
GETWIKI 18 OCT 2015
M.R.M. Parrott
Biographies
GETWIKI 20 AUG 2014
CONNECT