Assert.h
Encyclopedia
assert.h is a header file
Header file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...

 in the standard library
C standard library
The C Standard Library is the standard library for the programming language C, as specified in the ANSI C standard.. It was developed at the same time as the C POSIX library, which is basically a superset of it...

 of the C programming language that defines the C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....

 macro assert. The macro implements an assertion
Assertion (computing)
In computer programming, an assertion is a predicate placed in a program to indicate that the developer thinks that the predicate is always true at that place.For example, the following code contains two assertions:...

, which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false. In C++ it is also available through the cassert header file.

When executed, if the expression is false (that is, compares equal to 0), assert writes information about the call that failed on stderr and then calls abort
Abort (computing)
In a computer or data transmission system, to abort means to terminate, usually in a controlled manner, a processing activity because it is impossible or undesirable for the activity to proceed. Such an action may be accompanied by diagnostic information on the aborted process.In addition to being...

. 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
    C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...

    )
  • the text of expression that evaluated to 0


Example output of a program compiled with gcc
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

on Linux:

program: program.c:5: main: Assertion `a != 1' failed.
Abort (core dumped)

Programmers can eliminate the assertions without changing the source code:
if the macro NDEBUG is defined before the inclusion of , the assert macro is defined simply as:

#define assert(ignore)((void) 0)

and therefore has no effect on the program, 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.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK