C Standard function library --- & gt; assert. h

Source: Internet
Author: User

Assert. h is the header file in the C standard function library. The assert () macro is defined for program debugging.
In the C standard function library, it is a very special header file. You can introduce it several times for different effects. This effect depends on whether NDEBUG is defined during the introduction.
 
Macro
Assert () is a diagnostic macro used to dynamically identify logical error conditions of a program. Its prototype is void assert (int expression );
If the macro parameter evaluate result is a non-zero value, no action is performed. If it is a zero value, the diagnostic message is printed with a wide character (wide characters, then call abort (). Diagnostic messages include:
Source FILE Name (macro _ FILE _ value declared in stdlib. h)
The row number of the source file (the macro _ LINE _ value declared in stdlib. h)
Function Name (macro _ func _ value declared in stdlib. h), which is a new feature of C99.
Expression in which the result is 0
The display target dependency of the diagnostic information and the type of the called program. For a console program, the diagnostic information is displayed on the stderr device. For a window-based program, assert () generates a Windows MessageBox to display the diagnostic information.
The program can block all assert () without modifying the source code. You only need to add the macro-defined command line option to define the DNDEBUG Macro when calling the C language compiler through the command line. You can also introduce the <assert. h> previously, we used # define NDEBUG to define the macro. The blocked assert () does not even evaluate the parameter expression passed to it. Therefore, when assert () is used, its parameter expression cannot have side effects ).
 
Routine:
[Cpp]
# Include <stdio. h>
# Include <assert. h>

Int main (void)
{
FILE * fd;

Fd = fopen ("/home/user/file.txt", "r ");
Assert (fd );
Fclose (fd );

Return 0;
}

Routine:
[Cpp]
// NDEBUG is not defined
 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <assert. h>
 
Int main ()
 
{
 
Printf ("1 OK hello \ n ");
 
Assert (1 = 4 );
 
Printf ("2 OK exit \ n ");
 
Return 0;
 
}
[Cpp]
Result:
**************************************** **************************************** ******************
1 OK hello
Assert_h_ex_nodebug: assert_h_ex.c: 7: main: Assertion '1 = 4' failed.
Abandoned
**************************************** **************************************** ******************
[Cpp]
// Define NDEBUG
 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Define NDEBUG
 
# Include <assert. h>
 
Int main ()
 
{
 
Printf ("1 OK hello \ n ");
 
Assert (1 = 4 );;
 
Printf ("2 OK exit \ n ");
 
Return 0;
 
}
[Cpp]
Result:
**************************************** **************************************** **************************************** ********
1 OK hello
2 OK exit
**************************************** **************************************** **************************************** ********
[Cpp]
Principle:
# Define assert (test) if (! (Test ))\
 
Fprintf (stderr, "the failed: % s file % s, line % I \ n", # test, _ FILE __,__ LINE __);\
 
Abort ();
[Cpp]
Simulation:
 
# Include <stdio. h>
 
# Include <stdlib. h>
 
// # Define NDEBUG
 
// # Include <assert. h>
 
# Define Assert (test) if (! (Test) fprintf (stderr, "Assertion failed: % s, file % s, line % I \ n", # test, _ FILE __, _ LINE _); abort ()
 
Int main ()
 
{
 
Printf ("1 OK hello \ n ");
 
Assert (1 = 4 );
 
Printf ("2 OK exit \ n ");
 
Return 0;
 
}
[Cpp]
Result:
**************************************** **************************************** *****************
1 OK hello
Assertion failed: 1 = 4, file assert_h_ex.c, line 9
Abandoned
**************************************** **************************************** ******************
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.