#ifdef endif 用法
2016-07-26 11:35
399 查看
"#ifdef 语句1 程序2 #endif“ 可翻译为:如果宏定义了语句1则程序2。 作用:我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。 代码举例:新建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,在文件中加入代码: #define DEBUG #ifdef DEBUG #endif 而在define.cpp源文件中,代码修改如下: #include "iostream.h" #include "head.h" #define DEBUG int main() { #ifdef DEBUG cout<< "Beginning execution of main()"; #endif return 0; } 运行结果如下: Beginning execution of main() Press any key to continue 结论: 通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
相关文章推荐
- Android Studio设置颜色字体大小
- java文件下载文件名乱码
- Android常用的组件间通信方式
- 图的存储
- LeetCode 3Sum Closest JS实现
- 移动端遇到的问题及解决方案 table
- ajax之无刷新验证用户名
- windows Server2012 之 IIS8.0配置安装完整教程
- POJ-3295-Tautology
- 自执行函数的问题
- 博客索引
- 图像上批量取样本点坐标
- jedis连接redis提示JedisConnectionException
- CSS3 Transform详解
- Java并发编程(20):并发新特性—Lock锁和条件变量(含代码)
- Android Material Design之Toolbar与Palette实践
- Android Studio快捷键一览
- 理清Activity、View及Window之间关系
- <iframe>和<frame>区别
- 属性动画