assert
void assert (int expression);
Evaluate assertion
If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false),
a message is written to the standard error device and abort is called, terminating the program execution.
The specifics of the message shown depend on the specific implementation in the compiler,
but it shall include: the expression whose assertion failed, the name of the source file,
and the line number where it happened. A usual expression format is:
Assertion failed: expression, file filename, line line number
This macro is disabled if at the moment of including assert.h a macro with the name NDEBUG has already been defined.
This allows for a coder to include many assert calls in a source code while debugging the program and then disable all of them for the production version by simply including a line like:
#define NDEBUG at the beginning of its code, before the inclusion of assert.h.
Therefore, this macro is designed to capture programming errors, not user or running errors,
since it is generally disabled after a program exits its debugging phase.
Parameters
expression
Expression to be evaluated. If this expression evaluates to 0, this causes an assertion failure that terminates the program.
Return Value
none
Example
/* assert example */
#include <stdio.h>
#include <assert.h>
void print_number(int* myInt) {
assert (myInt!=NULL);
printf ("%d\n",*myInt);
}
int main ()
{
int a=10;
int * b = NULL;
int * c = NULL;
b=&a;
print_number (b);
print_number (c);
return 0;
}
In this example, assert is used to abort the program execution if print_number is called with a null pointer as attribute.
This happens on the second call to the function, which triggers an assertion failure to signal the bug.
{
hyp:
I tried this example in VC6.0 ,it is easy to do.
one way to compile it is by VC++ ide,
the other way is using cmd by the use of "cl" with the help of PowerCmd.It is a good utility.
but in vs2010,it is difficult to build a project to make it compile and run.
#define NDEBUG
is a utility.
}
void assert (int expression);
Evaluate assertion
If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false),
a message is written to the standard error device and abort is called, terminating the program execution.
The specifics of the message shown depend on the specific implementation in the compiler,
but it shall include: the expression whose assertion failed, the name of the source file,
and the line number where it happened. A usual expression format is:
Assertion failed: expression, file filename, line line number
This macro is disabled if at the moment of including assert.h a macro with the name NDEBUG has already been defined.
This allows for a coder to include many assert calls in a source code while debugging the program and then disable all of them for the production version by simply including a line like:
#define NDEBUG at the beginning of its code, before the inclusion of assert.h.
Therefore, this macro is designed to capture programming errors, not user or running errors,
since it is generally disabled after a program exits its debugging phase.
Parameters
expression
Expression to be evaluated. If this expression evaluates to 0, this causes an assertion failure that terminates the program.
Return Value
none
Example
/* assert example */
#include <stdio.h>
#include <assert.h>
void print_number(int* myInt) {
assert (myInt!=NULL);
printf ("%d\n",*myInt);
}
int main ()
{
int a=10;
int * b = NULL;
int * c = NULL;
b=&a;
print_number (b);
print_number (c);
return 0;
}
In this example, assert is used to abort the program execution if print_number is called with a null pointer as attribute.
This happens on the second call to the function, which triggers an assertion failure to signal the bug.
{
hyp:
I tried this example in VC6.0 ,it is easy to do.
one way to compile it is by VC++ ide,
the other way is using cmd by the use of "cl" with the help of PowerCmd.It is a good utility.
but in vs2010,it is difficult to build a project to make it compile and run.
#define NDEBUG
is a utility.
}