Assert.h
Template:Short description Template:Lowercase title Template:C Standard Library assert.h is a header file in the C standard library. It defines the C preprocessor macro Template:C-lang 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 Template:C++ are available; both are functionally equivalent.Template:Sfn
Use
The Template:C-lang 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 __FILE__ and __LINE__, respectively.Template:Sfn Since C99, the name of the function the assert statement is included as (__FUNC__) and the expression itself.Template:Sfn In ANSI C, the expression in the Template:C-lang macro is defined as signed integer, although any expression that can be implicitly cast to a signed integer may be used. In C99, the Template:C-lang macro explicitly allows any scalar type.[1] Two common uses of the Template:C-lang macro are to assert that a pointer is not null and to ensure that an array index is in-bounds.[2]
Below is a program using the Template:C-lang macro. This program will always evaluate Template:C-lang as false, as Template:C-lang is a null pointer and does not point to a valid memory location:
#include <assert.h>
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 Template:C-lang macro changes depending on the definition of another macro, Template:C-lang. If Template:C-lang is defined as a macro name, the Template:C-lang macro is defined as Template:C-lang,Template:Sfn thus resulting in the macro not evaluating the expression. The use of Template:C-lang may affect the overall behavior of a program if one or more Template:C-lang statements contain side effects, as these statements are not evaluated.Template:Sfn
The Template:C-lang macro does not include an error message. However the comma operator can be used to add it to the printed expression, as in Template:C-lang.Template:Sfn
static_assert
The static_assert macro, added in C++11, serves a similar purpose to the assert macro. Unlike the assert macro, static_assert runs at compile-time rather than at runtime.Template:Sfn The original implementation used template hacks.Script error: No such module "Unsubst". The Template:C++ 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.Template:Sfn In C++17, this assertion failure message was made optional, and the subsequent message is omitted if not specified.Template:Sfn
In C11, the functionally equivalent declaration _Static_assert was added. assert.h defines static_assert as an alias for _Static_assert to ensure parity with C++.Template:Sfn In C23, _Static_assert was renamed to static_assert and the string literal argument was made optional.Template:SfnTemplate:Sfn Gnulib defines static_assert for platforms that do not use C11 and does not require Template:Tt to be included.[3]
References
Citations
Bibliography
- Script error: No such module "citation/CS1".
- Template:Cite report
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".
- Template:Cite report
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".
- Template:Cite report
- Template:Cite report
- Script error: No such module "citation/CS1".
- Script error: No such module "citation/CS1".