#ifdef的用法

#ifdef的用法

灵活使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
代码举例:新建define.cpp文件

#include "iostream.h"
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:Press any key to continue

改写代码如下:
#include "iostream.h"
#define DEBUG
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:Beginning execution of main()
Press any key to continue

更一般的情况是,#define语句是包含在一个特定的头文件中。
比如,新建头文件head.h,在文件中加入代码:

#ifndef DEBUG
#define DEBUG
#endif

而在define.cpp源文件中,代码修改如下:
#include "iostream.h"
#include "head.h"
int main(){
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果如下:Beginning execution of main()
Press any key to continue
结论:通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值