当前位置:去回答>百科问答>assert函数的原型

assert函数的原型

2024-05-16 23:07:45 编辑:join 浏览量:616

assert函数的原型

assert

Evaluates an expression and when the result is FALSE, prints a diagnostic message and aborts the program.

void assert( int expression );

Example

/* ASSERT.C: In this program, the analyze_string function uses

* the assert function to test several conditions related to

* string and length. If any of the conditions fails, the program

* prints a message indicating what caused the failure.

*/

#include

#include

#include

void analyze_string( char *string ); /* Prototype */

void main( void )

{

char test1[] = "abc", *test2 = NULL, test3[] = "";

printf ( "Analyzing string '%s'\n", test1 );

analyze_string( test1 );

printf ( "Analyzing string '%s'\n", test2 );

analyze_string( test2 );

printf ( "Analyzing string '%s'\n", test3 );

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 != '\0' ); /* Cannot be empty */

assert( strlen( string ) > 2 ); /* Length must exceed 2 */

}

标签:assert,原型,函数

版权声明:文章由 去回答 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.quhuida.com/answer/84708.html
热门文章