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
**************************************** **************************************** ******************