C language preprocessing three (conditional compilation--#if)

Source: Internet
Author: User

//#if Conditional Compilation//language packs typically used for each version of the product#include <stdio.h>#include<stdlib.h>//#都是预处理指令, the conditional expression must be in pre-processing//So the conditional expression must be a macro expression//Two-branch conditional compilation//#if Conditional Expressions//Code Snippet 1//#else//Code Snippet 2//#endif//#endif结束条件编译//#if, #else和C语言里的if else function, but time overhead is different//If else compiles all the code, the source will be longer and the compilation time will be longer//Large program size, more memory consumption, long running time//#if, #else只编译符合条件的语句, effectively reduce the compiled statements,//Shorten the length of the source code and shorten the program execution time//Multi-branch conditional compilation//#if conditional expression 1//Code Snippet 1// #elif conditional expression 2 //Code Snippet 2//#elif conditional Expression 3//Code Snippet 3//#elif conditional Expression 4//Code Snippet 4//#else//Code Snippet 5//#endif//detect if a macro is defined// #ifdef macro definition//Code Snippet 1//#endif//#ifdef m detects if the macro is defined, defines the execution code snippet 1, does not do anything without definition//#ifdef一般用于开启某个功能//detect if a macro is defined// #ifndef macro definition//Code Snippet 1//#endif//#ifndef m detects if the macro is defined, executes code snippet 1 without a definition, does not perform any action//#ifndef一般用于开启某个功能或者include re-include troubleshooting //For example//#ifndef OPENS//#define OPENS////define a function or perform an operation so that it is not possible to duplicate the definition of a function or operation, because this code is not executed if it is already defined//#endif#defineLangua ' d 'voidMain () {//Two-branch conditional compilation#iflangua== ' E '//The conditional expression here is a macro expressionprintf ("I am the character E, beg you elder brother let a horse! \ n");#elseprintf ("I'm the boss, you want to rebel! \ n");#endif    //Multi-branch conditional compilation#iflangua== ' E '//The conditional expression here is a macro expressionprintf ("I am the character E, beg you elder brother let a horse! \ n");#eliflangua== ' d 'printf ("I am the character D, is your brother horse's brother! \ n");#elseprintf ("I'm the boss, you want to rebel! \ n");#endif    //detect if a macro is defined#defineM#ifdef M System ("Color 3f");#endif    //detect if a macro is defined#ifndef N System ("title Nihao");#endifSystem ("Pause");}

C language preprocessing three (conditional compilation--#if)

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.