C Standard Library -<assert.h>

Introduction

of the C Standard Libraryassert.hThe header file provides one calledassertThe macro can be used to verify assumptions made by the program and output diagnostic messages when the assumption is false.

The assert.h standard library is mainly used for assertions during program runtime. An assertion is a method for testing assumptions, usually used in the debugging phase, so as to immediately detect problems when the program enters an unexpected state.

<assert.h>The provided assertion mechanism is a useful tool in the C language, helping developers discover and fix errors in programs at an early stage.

Defined macrosassertpoints to another macroNDEBUG, macroNDEBUGIt is not part of <assert.h>. If NDEBUG is defined as a macro name in the source file that references <assert.h>, thenassertThe macro is defined as follows:

assert(expression)

The assert macro is used to test whether the expression expression is true. If expression is false (i.e., the result is 0), assert outputs an error message and terminates program execution. The error message includes the following:

  • The expression that triggers the assertion failure
  • source file name
  • line number

In release builds, all assert assertions can be disabled by defining NDEBUG. For example:

#define NDEBUG
#include <assert.h>

Once NDEBUG is defined, the assert macro will be preprocessed into an empty statement, with no runtime overhead.

The following is a simple example demonstrating the basic usage of assert:

Example

#include <stdio.h>
#include <assert.h>

void test_positive(int x) {
    assert(x > 0);
}

int main() {
    int a = 5;
    int b = -3;

    test_positive(a); // This assertion passes
    test_positive(b); // This assertion fails, the program terminates

    printf("This line will not be executed if an assertion fails.\n");

    return 0;
}

In the above example, when calling test_positive(b), since b is a negative number, the assertion x > 0 will fail, and the program will output information similar to the following and terminate:

Assertion failed: (x > 0), file example.c, line 6

Purpose of assertions

  • Debug: During the development phase, assertions can quickly detect logical errors in the program or situations where assumptions do not hold.
  • Documentation: Assertions can serve as part of the documentation, describing the preconditions and postconditions of functions.
  • Defensive programming: Although it is not recommended to use assertions for parameter checking in production code, in certain cases assertions can serve as a last line of defense.

Notes

  • Performance: In performance-sensitive code, assertions may add extra overhead, especially when called frequently. Therefore, assertions are usually disabled in release builds.
  • Error Handling: Assertions should not be used to handle error conditions that are expected and recoverable. Assertions are more often used to catch programmer errors.

Library macros

The following lists the only function defined in the header file assert.h:

Serial NumberFunctions & Descriptions
1void assert(int expression)
This is actually a macro, not a function, that can be used to add diagnostics in C programs.
other extensions