Use of assert in C language and assert in C Language

Source: Internet
Author: User

Use of assert in C language and assert in C Language
Assert

Function

The key points in the program can be asserted through the assert macro. If the assertions are not true, the program stops, and a standard error is returned, a specific error message is displayed.
However, in practice, it is not good to stop the program directly, so the assertions are only used by the debugging program.













NDEBUG macro

In order to facilitate the use of assert during debugging, it is easy to stop the work of assert when it is not needed. Therefore, the NDEBUG macro defines assert Behavior. If this macro is defined, assert does not work.

# Ifdef NDEBUG
# Define assert (x) (void) 0)
# Else
...








Header file features
The assert. h header file is different from other header files and does not have idempotence.
Idempotence means that the file is contained once to multiple times, and the effect is the same.


However, the assert. h file contains multiple times, but not necessarily the same.

At the beginning, the definition of the original assert macro was canceled.

Therefore, to open the assertions, you must write # undef NDEBUG # include <assert. h>
To close the assertions, write # define NDEBUG # include <assert. h>



Test

#include <stdio.h>#include <stdlib.h>#define NDEBUG#include <assert.h>void t1(void){    int i = 0;        assert(i == 0);    assert(i == 1);}#undef NDEBUG#include <assert.h>void t2(void){    int j = 0;        assert(j == 0);    assert(j == 1);}#define NDEBUG#include <assert.h>int main(void){    int i = 1;    assert(i == 0);    t1();    printf("hello world\n");    t2();}


When the t1 function starts, the assertions are canceled, so the execution of t1 will not stop. The assertions are implemented at the beginning of the t2 function, so the final t2 will stop at the time of the j = 1 sentence. Although the assertions of the main function are false, the assertions are canceled at the beginning and therefore will not be stopped.



Output result:












What is C language assert used?

Call the assert macro in assert. h.

Usage Error Detection
Assert (maxval (5, ten, sqr) = 50); the return value of maxval (5, ten, sqr) is not equal to 50
Output Error and exit program ..................

# Include <assert. h> void assert (int exp );
Function: Macro assert () is used for error detection. If the expression result is zero, the macro writes the error message to STDERR and exits program execution. If macro NDEBUG has been defined, macro assert () will be ignored.

What is the role of ASSERT in C?

It is an important means for program debugging,
ASSERT (f)
In Debug mode, the expression in parentheses is calculated after each running operation. If the expression is 0, the execution is interrupted. A warning box is displayed. You can select "continue ", "retry" and "Ignore"
In Release mode, this statement is not compiled into the code.

ASSERT is generally used in the program to confirm the correctness of parameters. That is, when calling an internal function, the caller must ensure that the parameters are correct, you can use ASSERT to check whether the parameters meet the requirements.

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.