Interview Questions  

Go Back   Interview Questions > Interview Questions & Answers > Information Technology > Programming Languages > C

C C Interview Questions, Learn by sharing C Interview Questions asked in various companies, Get Career advices, Interview Procedures from C experts, Post asked C Interview Questions and Answers.

   

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 11:47 AM
Senior Member
 
Join Date: Feb 2008
Posts: 9,095
Default What is assert()

What is assert()?
Reply With Quote
  #2 (permalink)  
Old 08-14-2008, 11:30 AM
Junior Member
 
Join Date: Aug 2008
Posts: 11
Default

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


void assert(
int expression
);



Parameters:

expression
Expression (including pointers) that evaluates to nonzero or 0.



Example
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.


// crt_assert.c
// compile with: /c
#include
#include
#include

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 != '\0' ); // Cannot be empty
assert( strlen( string ) > 2 ); // Length must exceed 2
}

Sample Output

Analyzing string 'abc'
Analyzing string '(null)'
Assertion failed: string != NULL, file crt_assert.c, line 24

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


with reference to msdn library......
Reply With Quote
  #3 (permalink)  
Old 08-14-2008, 11:33 AM
Junior Member
 
Join Date: Aug 2008
Posts: 11
Default waht is an assert()

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


void assert(
int expression
);



Parameters:

expression
Expression (including pointers) that evaluates to nonzero or 0.

Example program:

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.


// crt_assert.c
// compile with: /c
#include
#include
#include

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 != '\0' ); // Cannot be empty
assert( strlen( string ) > 2 ); // Length must exceed 2
}


Sample Output

Analyzing string 'abc'
Analyzing string '(null)'
Assertion failed: string != NULL, file crt_assert.c, line 24

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Reply With Quote
Reply

Tags
c language, interview questions

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On