Again, we can see assert (0, Baidu ).
The assert () macro is used to ensure that a specific condition is met,
Usage: assert (expression); if the expression value is false, the entire program will exit and output an error message.
If the expression value is true, continue executing the following statement.
Before using this macro, the header file assert. h must be included.
For example, # include <stdio. h>
# Include <assert. h>
Void main () {float a, B;
Scan ("% F", & A, & B );
Assert (B! = 0 );
Printf ("% F \ n", a/B );}
The above program needs to calculate the value of a/B, So B is required! = 0, so assert () is used in the program to ensure B! = 0. If B = 0, the program exits.
Another paragraph
Assert is a macro in <assert>. When assert is used, a parameter is provided, that is, a true expression. The Preprocessor generates code to test the assertions. If the assertions are not true, an error message is sent to indicate what the assertions are and if they fail for a while, the program will terminate. It can be used to determine whether an operation is successful.
Excerpt from Dr. Lin's book on high-quality programming .~~~~~~~~~~~~~~~~~~~~~~~~
Programs are generally divided into debug and release versions. The debug version is used for internal debugging, And the release version is released to users.
Assert is a macro that only works in the debug version. It is used to check the situation where "no" occurs. The following is a memory replication program. During the running process, if the assert parameter is false, the program will be suspended (A dialog is usually prompted, description of where the assert is triggered ). // Copy the non-overlapping memory block void memcpy (void * pvto, void * pvfrom, size_t size) {void * pbto = (byte *) pvto; void * pbfrom = (byte *) pvfrom; Assert (pvto! =
Null & pvfrom! = NULL); While (size-> 0) * pbto ++ = * pbfrom ++; Return (pvto);} assert is not a hasty macro, in order not to cause any difference between the debug and release versions of the program, assert should not produce any side effects. So assert is not a function, but a macro. Programmers can regard assert as a harmless testing method that can be safely used in any system status. The following principles Use assertions: 1) Use assertions to capture exceptions that should not occur. Do not confuse the differences between illegal and wrong situations. The latter must exist and be handled.
2) Use assertions to confirm the function parameters. 3) when writing a function, you should repeat it and ask yourself: "What assumptions do I plan to make ?" Once the assumptions are determined, we need to use assertions to check the assumptions. 4) In general textbooks, programmers are encouraged to design error-proof programs, but remember that such programming style will conceal errors. When programming against errors, if "impossible to happen" does happen, Use assertions to trigger an alarm.