Assert is a macro definition. The prototype is defined in <assert. h>:
# Include <assert. h>
Void assert (int expression );
The function is to terminate the program execution if the conditional expression returns an error.
The disadvantage of using assert is that frequent calls will greatly affect program performance and increase additional overhead.
After debugging, you can disable the assert call by inserting # define NDEBUG before the # include <assert. h> statement. The sample code is as follows:
# Include <stdio. h>
# Define NDEBUG
# Include <assert. h>
Usage summary and precautions:
1) Check the validity (validity) of parameters using assertions at the function entrance ).
2) Each assert only tests one condition, because when multiple conditions are verified simultaneously, if the assert fails, it is impossible to intuitively determine which condition fails.
3) You cannot use statements that change the environment, because assert takes effect only on DEBUG. If so, problems will occur when the program is actually running.
Error: assert (I ++ <100)
This is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed.
Correct: assert (I <100)
I ++;
4) assert and the following statement should be empty for logical and visual consistency.