MSDN原文如是说:
Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program.
(判断一个表达式,如果结果为假,输出诊断消息并中止程序。)
void assert(
int expression
);
参数:Expression (including pointers) that evaluates to nonzero or 0.(表达式【包括指针】是非零或零)
原理:assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
MSDN示例程序
// crt_assert.c
// compile with: /c
#include <stdio.h>
#include <assert.h>
#include <string.h>
void analyze_string( char *string ); // Prototype
int main( void )
{
char test1[] = "abc", *test2 = NULL, test3[] = "";
printf ( "Analyzing string '%s'n", test1 ); fflush( stdout );
analyze_string( test1 );
printf ( "Analyzing string '%s'n", test2 ); fflush( stdout );
analyze_string( test2 );
printf ( "Analyzing string '%s'n", test3 ); fflush( stdout );
analyze_string( test3 );
}
// Tests a string to see if it is NULL,
// empty, or longer than 0 characters.
void analyze_string( char * string )
{
assert( string != NULL ); // Cannot be NULL
assert( *string != '