#include <assert.h> void assert(int expr);
assert
puts diagnostics into programs. expr
is an expression.
If the macro name NDEBUG
is undefined at the point in the source
file where <assert.h>
is included, then assert
expands to a
statement that tests the expression. If the expression is false (that
is, compares equal to 0), assert
writes the text of the argument,
the source filename as defined by __FILE__
, and the source line
number as defined by __LINE__
to stderr
(the standard error
file). It then calls abort
.
If NDEBUG
is defined at the point in the program where
<assert.h>
is included, then assert
expands to (void *)0
.
The diagnostic is in the format of a normal library diagnostic, with the following text:
Assertion failed:expr is the expression, number is the current value ofexpr
Interrupted while: Executing linenumber
ofsource-file
__LINE__
, and source-file is the current value of __FILE__
.
assert
has no return value.
assert
generates a library diagnostic. Because the quiet
function suppresses library diagnostics, it also suppresses assert
diagnostics.
You should suppress assertions by defining NDEBUG
, not by calling
quiet
; quiet
will have no effect on the run-time
overhead of verifying the assertions.
NDEBUG
is automatically defined by the compiler, unless
you use
the DEBug
option. Suppress this automatic definition by
using the UNdef
option or by using NDEBUG
in a #undef
preprocessor directive. If you use the DEBug
option, then
NDEBUG
is not automatically defined.
#include <math.h> #include <assert.h> #include <stdio.h> double arcsin(double x) { assert(x <= 1.0 && x >= -1.0); return asin(x); } main() { double num, svalue; puts("Enter a number."); scanf("%f",&num); svalue = arcsin(num); printf("The arcsin of the number is %f n", svalue); }
quiet
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.