ASSERT () usage

Source: Internet
Author: User
Tags prototype definition

The prototype of an Assert macro is defined in <assert.h>, and its function is to terminate the program execution if its condition returns an error, the prototype definition: [1]
#include <assert.h>
void assert (int expression);

The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.

//demo1.c#include <stdio.h>#include<assert.h>#include<stdlib.h>intDemo1 (intnum) {Assert (num> -); printf ("demo2:%d\n", num); return 0;}//demo2.c#include <stdio.h>#include<assert.h>#include<stdlib.h>intDemo2 (intnum) {Assert (num> $); printf ("demo2:%d\n", num); return 0;}//demo.c#include <stdio.h>#include<stdlib.h>intMainvoid) {demo1 ( the); Demo2 ( the)//Do not meet the num>200 conditions, the error is as followsreturn 0; }
test phenomenon: [email protected]:/work/demo# gcc *.c;. /a . Out  Demo2: out: demo2.c:7: demo2:assertion ' num> ' failed. Aborted

After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:

#include <stdio.h>
#define Ndebug
#include <assert.h>

Such as:

//demo2.c 
#include <stdio.h> #define ndebug // #include < Assert.h> #include <stdlib.h> int Demo2 (int num) {assert (num >< Span style= "color: #800080;" >200 demo2:%d\n " ,num); return 0 ;}
phenomenon: [email protected]:/work/demo# gcc *.c;. /a . Out Demo2:demo2:

Reference:

1. Summary of ASSERT () function usage
Http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html

ASSERT () usage

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.