#ifdef语句1
//程序2
#endif
可翻译为:如果宏定义了语句1则执行程序2。
作用:我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。
代码举例:新建define.cpp文件
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
#ifdef DEBUG
cout << "Beginning execution of main()" << endl;
#endif
return 0;
}
运行结果为:
Press any key to continue
改写代码如下:
#include <iostream>
using namespace std;