Usage of assert in C language

Source: Internet
Author: User
Tags assert

1 /*************************************************************************2 > File name:assert.c3 > Author:Mr.Yang4 > Purpose: Demonstrating the use of function assert5 > Created time:2017 May 29 Monday 19:57 54 seconds6  ************************************************************************/7 8#include <stdio.h>9 //#define NDEBUG Frequent calls can affect the performance of your program, and you can disable the Assert after debugging endsTen#include <assert.h>//use the Assert function to introduce package files One#include <stdlib.h> A  - intMainvoid) - { theFILE *FP; -  -         /*open in a written way*/ -fp = fopen ("Test.txt","W"); + assert (FP); - fclose (FP); +  A         /*Open in read-only mode*/ atfp = fopen ("Newtest.txt","R");//Newtest.txt is not there. - assert (FP); -Fclose (FP);//The program will never be executed here. -  -         return 0; - }

Execution Result:

Assert:assert.c:24:main:assertion ' FP ' failed.
has been abandoned

Function Prototypes:

#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.

Reference Source: http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html

Usage of assert in C language

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.