条件编译,可以用来有选择地对部分程序源代码进行编译
#ifdef DEBUG
cerr <<"Variable x = " << x << endl;
#endif
#ifdef DEBUG 之前已经定义了符号常量 DEBUG,则会对程序中的 cerr 语句进行编译。 #if 0 语句注释掉程序的一部分
#include <iostream>
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
int i, j;
i = 100;
j = 30;
#ifdef DEBUG
cerr <<"Trace: Inside main function" << endl;
#endif
#if 0
/* 这是注释部分 */
cout << MKSTR(HELLO C++) << endl;
#endif
cout <<"The minimum is " << MIN(i, j) << endl;
#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}
结果
Trace: Inside main function
The minimum is 30
Trace: Coming out of main function
2.# 和 ## 运算符