#if #ifdef #ifndef #undef 条件编译

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangjiaweiwei/article/details/86654039

1、#if命令

  #if的基本含义:如果#if命令后的参数表达式为真,则编译#if到#endif之间的程序段,#endif

命令用来表示#if段的结束,否则跳过这段程序。

  #if命令的一般格式如下:

  

#if 常数表达式
    语句段
#endif

如果“常数表达式”为真,则该段程序被编译。

例子:

#include <stdio.h>

#define NUM 50

int main(void) { 
      
    int i = 0;
    #if NUM > 50
     i ++;
    #endif
    
    #if NUM == 50
     i = i + 50;
    #endif
    
    #if NUM < 50
     i --;
    #endif
      
    printf("Now i is :%d\n", i); 
    

	return 0;
}

运行结果:

Now i is :50

如果将语句

#define NUM 50

改为:

#define NUM 10

则程序运行结果如下:

Now i is :-1

同样,如果将语句

#define NUM 50

改为:

#define NUM 100

则程序运行结果为:

Now i is :1

#else 的作用是为#if为假时提供另一种选择,#else的使用例子:

#include <stdio.h>

#define NUM 50

int main(void) { 
      
    int i = 0;
    #if NUM > 50
     i ++;
    
    #else
    #if NUM == 50
     i = i + 50;
    
    #else
     i --;
     
    #endif
    #endif
      
    printf("Now i is :%d\n", i); 
    

	return 0;
}

程序运行结果为:

Now i is :50

   #elif 命令用来建立一种“如果......或者如果......”阶梯状多重编译操作选择,这与多分支if语句中的

else if 类似。

  #elif命令的一般格式如下:

#if 表达式
语句段
#elif 表达式1
语句段
#elif 表达式2
语句段
...
#elif 表达式n
语句段
#endif

上面的示例可以修改为:

#include <stdio.h>

#define NUM 50

int main(void) { 
      
    int i = 0;
    #if NUM > 50
     i ++;
    
    #elif NUM == 50
     i = i + 50;
    
    #else
     i --;
     
    #endif
      
    printf("Now i is :%d\n", i); 
    

	return 0;
}

运行结果不变:

Now i is :50

2、#ifdef 和 #ifndef命令

  前面介绍的#if条件编译命令中,需要判断符号常量所定义的具体值,但有时并不需要判断具体值。

只要知道这个符号常量是否被定义了,这时就不需要使用#if,而采用另一种条件编译的办法,即#ifdef

与#ifndef命令,它们分别表示“如果有定义”及“如果无定义”。

  #ifdef的一般格式如下:

#ifdef 宏替换名
     语句段
#endif

     其含义是:如果“宏替换名”已被定义过,则对“语句段”进行编译,如果没有定义#ifdef后面的“宏替换名”,

则不过“语句段”进行编译。

   #ifdef 可与#else连用,构成的一般格式如下:

#ifdef 宏替换名
   语句段1
#else
   语句段2
#endif

  其含义是:如果“宏替换名”已被定义过,则对“语句段1”进行编译;如果没有定义#ifdef后面的“宏替换名”,

则对“语句段2”进行编译。 

#ifndef的一般格式如下:

#ifndef 宏替换名
    
   语句段

#endif

     其含义是:如果未定义#ifndef后面的“宏替换名”,则对“语句段1”进行编译;如果定义了#ifndef后面的“宏替换名”,

则不执行语句段。

  同样,#ifndef也可以与#else连用,构成的一般格式如下:

#ifndef 宏替换名
    
   语句段1

#else

   语句段2
  

#endif

    其含义是:如果未定义#ifndef后面的“宏替换名”,则对“语句段1”进行编译;如果定义了#ifndef后面的“宏替换名”,

则对“语句段2”进行编译。

   

#include <stdio.h>

#define STR  "diligence is the parent of success\n"

int main(void) { 
      
    #ifdef STR
    printf(STR);
    #else
    printf("idleness is the root of all evil\n");
    #endif
    
    printf("\n");
    
    #ifndef ABC
    printf("idleness is the root of all evil\n");
    #else
    printf(STR);
    #endif

	return 0;
}

运行结果为:

diligence is the parent of success

idleness is the root of all evil

3、#undef命令

#undef命令的一般格式如下:

#undef 宏替换名
#include <stdio.h>

#define NUM 50

int main(void) { 
      
    int i = 0;
    #ifdef NUM
     
     i = 50;
     
    #endif
      
    printf("Now i is :%d\n", i); 
    
    #undef NUM
    
     i = 0;
   
    #ifdef NUM 
    
     i  = 50;
    
    #endif
    
    printf("Now i is :%d\n", i); 
    

	return 0;
}

说明:#undef的主要目的是将宏名局限在仅需要它们的代码段中,比如上例#undef之后 NUM定义失效,i的值没有变成50。

4、#line命令

    #line命令用来改变_LINE_ 和 _FILE_的内容(_LINE_中存放的是当前编译行的行号,_FILE_中存放的

是当前编译的文件名),主要用于调试及其他特殊应用。

   #line的一般格式如下:

#line 行号["文件名"]

  其中,“行号”为任一正整数,代表源程序中当前行号;可选的“文件名”为任一有效文件标识符,代表源文件的名称。

#line 100 "13.7.C"
#include <stdio.h>


int main(void) { 
      
   printf("1.The current line NO.:%d\n", __LINE__);
   printf("2.The current line NO.:%d\n", __LINE__);
   
   printf("3.The current file name is:%s\n", __FILE__);

	return 0;
}

输出结果为:

1.The current line NO.:105
2.The current line NO.:106
3.The current file name is:13.7.C

可以看到,可以通过#line改变行号和文件名

猜你喜欢

转载自blog.csdn.net/wangjiaweiwei/article/details/86654039