C language Conditional compilation

Source: Internet
Author: User

C language pre-compilation includes three types: 1. macro definition 2. file contains 3. Conditional compilation, which refers to compiling only when certain conditions are met. It has several forms:

(1) # ifdef identifier

// Program

# Else

// Program

# Endif

It means that if an identifier is defined, the execution segment 1; otherwise, the execution Segment 2

Or in the following format:

# Ifdef identifier

// Program

# Endif

# include 
 
  # include 
  
   int main(){#ifdef DEBUGprintf("debug is running\n");#elseprintf("debug is not running\n");#endif    system("pause");    return 0;}
  
 

(2)

# Ifndef identifier

// Program 1

# Else

// Program 2

# Endif

It means that if the identifier is not defined, the execution segment 1; otherwise, the execution Segment 2

# include 
 
  # include 
  
   int main(){#ifndef DEBUGprintf("debug is not running\n");#elseprintf("debug is  running\n");#endif    system("pause");    return 0;}
  
 

(3) # if expression

// Program 1

# Else

// Program 2

# Endif

It indicates that when the expression value is true, the program segment 1 will be compiled. When the expression value is false, the program segment 2 will be compiled.

# include 
 
  # include 
  
   # define HEX 1int main(){int i=10;#if HEX==1printf("%x\n",i);#elseprintf("%d\n",i);#endifsystem("pause");return 0;}
  
 


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.