打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
#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指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
预编译#define #ifdef #endif
C++ 预处理器
"_DEBUG"与"DEBUG"的区别
Linux下syslog日志函数使用
宏总结
trace
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服