Assert.h
   HOME

TheInfoList



OR:

assert.h is a
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
in the
standard library In computer programming, a standard library is the library made available across implementations of a programming language. These libraries are conventionally described in programming language specifications; however, contents of a language's as ...
of the
C programming language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
that defines the
C preprocessor The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control ...
macro assert().International Standard for Programming Language C (C99), ISO/IEC 9899:1999, p. 169 In C++ it is also available through the <cassert> header file.


Assert

assert(a != 1); This is a macro that implements a runtime assertion, which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false. When executed, if the expression is false (that is, compares equal to 0), assert() will write information about the call that failed on
stderr In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
and then call abort(). The information it writes to stderr includes: * the source filename (the predefined macro __FILE__) * the source line number (the predefined macro __LINE__) * the source function (the predefined identifier __func__) (added in
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
) * the text of expression that evaluated to 0 Example output of a program compiled on Linux: program: program.c:5: main: Assertion `a != 1' failed. Abort (core dumped) Programmers can eliminate the assertions just by recompiling the program, without changing the source code: if the macro NDEBUG is defined before the inclusion of , the assert() macro may be defined simply as: #define assert(ignore)((void) 0) and therefore has no effect on the compilation unit, not even evaluating its argument. Therefore expressions passed to assert() must ''not'' contain
side-effect In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
s since they will not happen when debugging is disabled. For instance: assert(x = gets()); will not read a line and not assign to x when debugging is disabled.


Additional message

There is no standardized variant of assert() that includes an error message. This can nevertheless be achieved using a
comma operator In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there ...
, which discards all preceding values and only keeps the last one: assert(("Orwellian", 2 + 2

5));
Will yield something similar to: program: program.c:5: main: Assertion `("Orwellian", 2 + 2

5)' failed. Abort A more convenient syntax can be made with a macro, here using the name Microsoft uses for a similar macro: #define _ASSERT_EXPR(test, message) assert(((void)(message), test)) _ASSERT_EXPR(2 + 2

5, "Orwellian");


Static assert

static_assert(sizeof(int) > 20, "I need huge integers");
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
added a similar keyword static_asserthttps://en.cppreference.com/w/cpp/language/static_assert that contextually converts a constant expression to bool and prints a message (optional since C++17) at compile-time if it is false. It is possible to simulate this using a macro and templates, though this is probably not necessary since most modern C++ compilers support this C++11 feature. This feature was formally added in
C11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build var ...
as the keyword _Static_assert with an identical usage, and in a convenience macro static_assert is added. It is possible to simulate a static assertion in older versions of C using a macro: , though the resulting error is cryptic. (It triggers an error because C allows zero-length arrays, but not negative-length ones.)
Gnulib Gnulib, also called the GNU portability library, is a collection of software subroutines which are designed to be usable on many operating systems. The goal of the project is to make it easy for free software authors to make their software run ...
's version of the static assert uses sizeof and a structure to trigger a similar error.


Example

#include #include int main() i = 0 i = 1 i = 2 i = 3 i = 4 assert: example.c:10: main: Assertion `i < 5' failed. Aborted


External links

*


References

{{Reflist C standard library headers