1 #define max 2 #define maximum(x,y) (x>y)?x:y 3 #define minimum(x,y) (x>y)?y:x 4 void main() 5 { int a=10,b=20; 6 #ifdef max 7 printf("\40: the larger one is %d\n",maximum(a,b)); 8 #else 9 printf("\40: the lower one is %d\n",minimum(a,b)); 10 #endif 11 #ifndef min 12 printf("\40: the lower one is %d\n",minimum(a,b)); 13 #else 14 printf("\40: the larger one is %d\n",maximum(a,b)); 15 #endif 16 #undef max 17 #ifdef max 18 printf("\40: the larger one is %d\n",maximum(a,b)); 19 #else 20 printf("\40: the lower one is %d\n",minimum(a,b)); 21 #endif 22 #define min 23 #ifndef min 24 printf("\40: the lower one is %d\n",minimum(a,b)); 25 #else 26 printf("\40: the larger one is %d\n",maximum(a,b)); 27 #endif 28 }
#ifdef 标识符
程序段1
#else
程序段2
#endif
它的作用是:当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2。
其中#else部分也可以没有,即:
#ifdef
程序段1
#denif
这里的“程序段”可以是语句组,也可以是命令行。
#ifndef 标识符
程序段1
#else
程序段2
#endif
只是第一行与第一种形式不同:将“ifdef”改为“ifndef”。它的作用是:若标识符未被定义则编译程序段1,
否则编译程序段2。这种形式与第一种形式的作用相反。
还有一种形式,就是#if后面的是一个表达式,而不是一个简单的标识符:
#if 表达式
程序段1
#else
程序段2
#endif
它的作用是:当指定的表达式值为真(非零)时就编译程序段1,否则编译程序段2。可以事先给定一定条
件,使程序在不同的条件下执行不同的功能。
转载引用:https://www.cnblogs.com/yanwei-wang/p/8073114.html 侵删
https://blog.youkuaiyun.com/ddl2111/article/details/50835512 侵删