C-language static assertion-Define your own static assertions

Source: Internet
Author: User
Tags assert

C language inside can define the static assertion, more convenient debugging code.

Using static assertions

1#include <stdio.h>2#include <stdlib.h>3#include <assert.h>4 5 voidMain ()6 {7     intnum = -;8ASSERT (Num < -);//num<20 is normal, otherwise abnormal9printf"%d", num);Ten}

Using your own defined static assertions to implement the

1#include <stdio.h>2#include <stdlib.h>3#include <assert.h>4 5 //define line can not be used to write6 //pre-compilation processing7 8 #defineDEBUG9#ifndef DEBUG//If you do not define a constant debugTen #defineMyassert (x)//definition only, does not write code detection hints One #else A //define Myassert and write code detection hints - #defineMyassert (x) - if(!(x)) the     { -printf"Myassert (%s) macro started detection .... \ n", #x); -printf"It's a mistake."); -printf"The current function name is%s, the file name is%s and the code line number is%d", __function__,__file__,__line__); +     } - #endif +  A  at voidMain () - { -     intnum = -; -ASSERT (Num < -);//num<20 is normal, otherwise abnormal -printf"%d", num); -}

  

C-language static assertion-Define your own static assertions

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.