Assert.h
   HOME

TheInfoList



OR:

assert.h is a
header file An include directive instructs a text file processor to replace the directive text with the content of a specified file. The act of including may be logical in nature. The processor may simply process the include file content at the location of ...
in the
C standard library The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
. It defines the
C preprocessor The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control. Although ...
macro and implements runtime assertion in C. assert.h is defined in
ANSI C ANSI C, ISO C, and Standard C are successive standards for the C programming language published by the American National Standards Institute (ANSI) and ISO/IEC JTC 1/SC 22/WG 14 of the International Organization for Standardization (ISO) and the ...
as part of the C standard library. In the C++ programming language, assert.h and are available; both are functionally equivalent.


Use

The macro implements runtime assertion. If the expression within it is false, the macro will print a message to
stderr In computer programming, standard streams are preconnected 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 call abort(), defined in stdlib.h. The message includes the source filename and the source line number from the macros and , respectively. Since
C99 C99 (previously C9X, formally ISO/IEC 9899:1999) is a past version of the C programming language open standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps implementations mak ...
, the name of the function the assert statement is included as () and the expression itself. In ANSI C, the expression in the macro is defined as
signed integer In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are ...
, although any expression that can be implicitly cast to a signed integer may be used. In C99, the macro explicitly allows any
scalar Scalar may refer to: *Scalar (mathematics), an element of a field, which is used to define a vector space, usually the field of real numbers *Scalar (physics), a physical quantity that can be described by a single element of a number field such a ...
type. Two common uses of the macro are to assert that a pointer is not null and to ensure that an array index is in-bounds. Below is a program using the macro. This program will always evaluate as false, as is a
null pointer In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value saved for indicating that the Pointer (computer programming), pointer or reference (computer science), reference does not refer to a valid Object (c ...
and does not point to a valid
memory location In computing, a memory address is a reference to a specific memory location in memory used by both software and hardware. These addresses are fixed-length sequences of digits, typically displayed and handled as unsigned integers. This numeric ...
: #include int main() 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 macro changes depending on the definition of another macro, . If is defined as a macro name, the macro is defined as , thus resulting in the macro not evaluating the expression. The use of may affect the overall behavior of a program if one or more statements contain
side effect In medicine, a side effect is an effect of the use of a medicinal drug or other treatment, usually adverse but sometimes beneficial, that is unintended. Herbal and traditional medicines also have side effects. A drug or procedure usually use ...
s, as these statements are not evaluated. The macro does not include an
error message An error message is the information displayed when an unforeseen problem occurs, usually on a computer or other device. Modern operating systems with graphical user interfaces, often display error messages using dialog boxes. Error messages are us ...
. However the
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 ...
can be used to add it to the printed expression, as in .


static_assert

The macro, added in
C++11 C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
, serves a similar purpose to the macro. Unlike the macro, runs at
compile-time In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
rather than at runtime. The original implementation used template hacks. The 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. In
C++17 C17, C-17 or C.17 may refer to: Transportation * , a 1917 British C-class submarine Air * Boeing C-17 Globemaster III, a military transport aircraft * Lockheed Y1C-17 Vega, a six-passenger monoplane * Cierva C.17, a 1928 English experimental ...
, this assertion failure message was made optional, and the subsequent message is omitted if not specified. In C11, the functionally equivalent declaration was added. assert.h defines as an alias for to ensure parity with C++. In C23, was renamed to and the string literal argument was made optional.
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 o ...
defines for platforms that do not use C11 and does not require to be included.


References


Citations


Bibliography

* * * * * * * * * * * {{Cite book , last=Swaminathan , first=Jeganathan , date=2017 , title=Mastering C++ Programming , location=Birmingham , publisher=
Packt Packt is a publishing company founded in 2003 and headquartered in Birmingham, UK, with offices in Mumbai, India. Packt primarily publishes print and electronic books and videos relating to information technology, including programming, web ...
, isbn=9781786461629 C standard library headers