On the use of assert in C language _c language

Source: Internet
Author: User
Tags assert terminates

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 <stdio.h>
#include <assert.h>
#include <stdlib.h>
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.
Fclose (FP);

fp = fopen ("Noexitfile.txt", "R"),//open a file as read-only, failure to open file if not present
ASSERT (FP); So there'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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
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 <assert.h>
void assert (int expression);

The function of an assert is to first evaluate an expression expression, if its value is false (that is, 0), it first flows to the standard error stderrPrint an error message, and then call the AbortTo terminate the program, 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.