Use of Assert in C language

Source: Internet
Author: User
Tags assert expression terminates
The following is an introduction to the use of Assert in C language, the need for friends can refer to the next

The stereotype of an Assert macro is defined in <assert.h>, which terminates the execution of the program if its condition returns an error, and the prototype defines:
#include <assert.h>
void assert (int expression);
The function of assert is to calculate the expression expression, if its value is false (that is, 0), then it first prints an error message to stderr.
The program is then terminated by calling abort.
please see the following list of programs BADPTR.C:

Copy Code code as follows:


#include &lt;stdio.h&gt;


#include &lt;assert.h&gt;


#include &lt;stdlib.h&gt;


int main (void)


{


FILE *FP;





fp = fopen ("Test.txt", "w"),//open a file in a writable manner, and create a file with the same name if it does not exist


assert (FP); So there's no mistake here


Fclose (FP);





fp = fopen ("Noexitfile.txt", "R");//Open a file as read-only and fail to open file if not present


assert (FP); So here's a mistake


fclose (FP); The program will never be executed here.


return 0;


}


Macro Name: Assert
function: Test a condition and possibly terminate the program
Usage: void assert (int test);
Program Example:

Copy Code code as follows:


#include &lt;assert.h&gt;


#include &lt;stdio.h&gt;


#include &lt;stdlib.h&gt;


struct ITEM {


int key;


int value;


};





/* Add item to list, make sure list are NOT NULL */


void AddItem (struct ITEM *itemptr) {


assert (Itemptr!= NULL);


/* Add Item to List * * *


}





int main (void)


{


additem (NULL);


return 0;


}


assert () macro usage
Note: Assert is a macro, not a function. In the assert.h header file in C.
The stereotype of an Assert macro is defined in <assert.h>, which terminates the execution of the program if its condition returns an error, and the prototype defines:

Copy Code code as follows:


#include &lt;assert.h&gt;


void assert (int expression);

The


assert works by first evaluating the expression expression , and if its value is false (that is, 0), it first prints an error message to the standard error stream stderr . The program is then terminated by calling Abort , otherwise the ASSERT () has no effect. The macro assert () is generally used to confirm the normal operation of the program, where the expression is constructed in a true truth. After debugging, you do not have to remove the ASSERT () statement from the source code because the definition of the macro assert () is null when the macro ndebug is defined.

Related Article

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.