assert.h

From Wikipedia, the free encyclopedia

assert.h is a header file in the standard library of the C programming language that defines the C preprocessor macro assert().[1] [2] 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 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)
  • the text of expression that evaluated to 0 [1]

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 <assert.h>, 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-effects 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, 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[3] 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 added a similar keyword static_assert[4] that contextually converts a constant expression to bool and prints a message (optional since C++17[4]) 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 as the keyword _Static_assert with an identical usage, and in <assert.h> a convenience macro static_assert is added.

It is possible to simulate a static assertion in older versions of C using a macro: #define static_assert(cond, str) char _temp[-!((void)str, (cond))], though the resulting error is cryptic. (It triggers an error because C allows zero-length arrays, but not negative-length ones.) Gnulib's version of the static assert uses sizeof and a structure to trigger a similar error.[5]

Example[]

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

int main()
{
    int i;

    for (i=0; i<=9; i++)
    {
        assert(i <= 4);
        printf("i = %d\n", i);
    }

    return 0;
}
i = 0
i = 1
i = 2
i = 3
i = 4
assert: example.c:10: main: Assertion `i <= 4' failed.
Aborted

External links[]

  • assert.h: verify program assertion – Base Definitions Reference, The Single UNIX Specification, Issue 7 from The Open Group

References[]

  1. ^ a b International Standard for Programming Language C (C99), ISO/IEC 9899:1999, p. 169
  2. ^ [Coding Programmer Page C / C++ Reference]. Archived from the original on 2012-06-30. Retrieved 2012-03-23.
  3. ^ "_ASSERT, _ASSERTE, _ASSERT_EXPR Macros".
  4. ^ a b https://en.cppreference.com/w/cpp/language/static_assert
  5. ^ "gnulib/lib/verify.h". coreutils. 24 November 2019.
Retrieved from ""