Assert.h

Last updated

assert.h is a header file in the C standard library. It defines the C preprocessor macro assert and implements runtime assertion in C.

Contents

assert.h is defined in ANSI C as part of the C standard library. In the C++ programming language, assert.h and <cassert> are available; both are functionally equivalent. [1]

Use

The 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 __FILE__ and __LINE__, respectively. [2] Since C99, the name of the function the assert statement is included as (__FUNC__) and the expression itself. [3] In ANSI C, the expression in the 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 assert macro explicitly allows any scalar type. [4] Two common uses of the assert macro are to assert that a pointer is not null and to ensure that an array index is in-bounds. [5]

Below is a program using the assert macro. This program will always evaluate pointer as false, as pointer is a null pointer and does not point to a valid memory location:

#include<assert.h>intmain(){void*pointer=0;assert(pointer);return0;}

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 assert macro changes depending on the definition of another macro, NDEBUG. If NDEBUG is defined as a macro name, the assert macro is defined as #define assert(ignore) ((void)0), [3] thus resulting in the macro not evaluating the expression. The use of NDEBUG may affect the overall behavior of a program if one or more assert statements contain side effects, as these statements are not evaluated. [6]

The assert macro does not include an error message. However the comma operator can be used to add it to the printed expression, as in assert(("Not Orwellian",2+2==5));. [7]

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. [8] The original implementation used template hacks.[ citation needed ] The 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. [8] In C++17, this assertion failure message was made optional, and the subsequent message is omitted if not specified. [9]

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++. [10] In C23, _Static_assert was renamed to static_assert and the string literal argument was made optional. [11] [12] Gnulib defines static_assert for platforms that do not use C11 and does not require assert.h to be included. [13]

Related Research Articles

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 International Electrotechnical Commission (IEC). Historically, the names referred specifically to the original and best-supported version of the standard. Software developers writing in C are encouraged to conform to the standards, as doing so helps portability between compilers.

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, protocol stacks, though decreasingly for application software. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.

<span class="mw-page-title-main">COBOL</span> Programming language with English-like syntax

COBOL is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily used in business, finance, and administrative systems for companies and governments. COBOL is still widely used in applications deployed on mainframe computers, such as large-scale batch and transaction processing jobs. However, due to its declining popularity and the retirement of experienced COBOL programmers, programs are being migrated to new platforms, rewritten in modern languages or replaced with software packages. Most programming in COBOL is now purely to maintain existing applications; however, many large financial institutions were still developing new systems in COBOL as late as 2006.

<span class="mw-page-title-main">C++</span> General-purpose programming language

C++ is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programming language, it has since expanded significantly over time; modern C++ currently has object-oriented, generic, and functional features, in addition to facilities for low-level memory manipulation. It is almost always implemented as a compiled language, and many vendors provide C++ compilers, including the Free Software Foundation, LLVM, Microsoft, Intel, Embarcadero, Oracle, and IBM.

The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.

In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.

The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C library POSIX specification, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.

The null character is a control character with the value zero. It is present in many character sets, including those defined by the Baudot and ITA2 codes, ISO/IEC 646, the C0 control code, the Universal Coded Character Set, and EBCDIC. It is available in nearly all mainstream programming languages. It is often abbreviated as NUL. In 8-bit codes, it is known as a null byte.

In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. This is different from unspecified behavior, for which the language specification does not prescribe a result, and implementation-defined behavior that defers to the documentation of another component of the platform.

<span class="mw-page-title-main">C99</span> C programming language standard, 1999 revision

C99 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 implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology. The C11 version of the C programming language standard, published in 2011, replaces C99.

In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

The C0 and C1 control code or control character sets define control codes for use in text by computer systems that use ASCII and derivatives of ASCII. The codes represent additional information about the text, such as the position of a cursor, an instruction to start a new line, or a message that the text has been received.

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 the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

ISO/IEC JTC 1/SC 22 Programming languages, their environments and system software interfaces is a standardization subcommittee of the Joint Technical Committee ISO/IEC JTC 1 of the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) that develops and facilitates standards within the fields of programming languages, their environments and system software interfaces. ISO/IEC JTC 1/SC 22 is also sometimes referred to as the "portability subcommittee". The international secretariat of ISO/IEC JTC 1/SC 22 is the American National Standards Institute (ANSI), located in the United States.

C11 is an informal name for ISO/IEC 9899:2011, a past standard for the C programming language. It replaced C99 and has been superseded by C17. C11 mainly standardizes features already supported by common contemporary compilers, and includes a detailed memory model to better support multiple threads of execution. Due to delayed availability of conforming C99 implementations, C11 makes certain features optional, to make it easier to comply with the core language standard.

In the C++ programming language, decltype is a keyword used to query the type of an expression. Introduced in C++11, its primary intended use is in generic programming, where it is often difficult, or even impossible, to express types that depend on template parameters.

<span class="mw-page-title-main">OpenLisp</span>

OpenLisp is a programming language in the Lisp family developed by Christian Jullien from Eligis. It conforms to the international standard for ISLISP published jointly by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), ISO/IEC 13816:1997(E), revised to ISO/IEC 13816:2007(E).

ISO/IEC JTC 1/SC 37 Biometrics is a standardization subcommittee in the Joint Technical Committee ISO/IEC JTC 1 of the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC), which develops and facilitates standards within the field of biometrics. The international secretariat of ISO/IEC JTC 1/SC 37 is the American National Standards Institute (ANSI), located in the United States.

ISO/IEC JTC 1/SC 17 Cards and personal identification is a standardization subcommittee of the Joint Technical Committee ISO/IEC JTC 1 of the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC), which develops and facilitates standards within the field of identification cards and personal identification. The international secretariat of ISO/IEC JTC 1/SC 17 is the British Standards Institution (BSI) located in the United Kingdom.

C23 is the informal name for ISO/IEC 9899:2023, the next standard for the C programming language, which will replace C17. It was started in 2016 informally as C2x, and expected to be published in 2024. The most recent publicly available draft of C23 was released on April 1, 2023. The first WG14 meeting for the C2x draft was held in October 2019, virtual remote meetings were held in 2020 due to the COVID-19 pandemic, then various teleconference meetings continued to occur through 2023.

References

Citations

  1. Binder 2000, p. 860.
  2. Kernighan & Ritchie 1988, p. 253-254.
  3. 1 2 ISO/IEC JTC 1/SC 22/WG14 1999, p. 169.
  4. "Linux Programmer's Manual". August 25, 2002. Retrieved March 14, 2023.
  5. Reekie, John (December 7, 1995). "How to use assertions in C". University of California, Berkeley . Retrieved March 14, 2023.
  6. American National Standards Institute 1990, p. 76.
  7. Gregoire 2021, p. 1058.
  8. 1 2 ISO/IEC JTC 1/SC 22/WG21 2012, p. 134.
  9. Swaminathan 2017, p. 13.
  10. Prata 2013, p. 762-763.
  11. Gustedt 2022, p. 3.
  12. Ballman & Grammatech 2018, p. 1.
  13. "GNU Gnulib". Free Software Foundation. February 6, 2023. Retrieved March 14, 2023.

Bibliography