欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

C++中 #if #endif 和#ifdef #endif的用法

发布时间:2024/1/8 65 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++中 #if #endif 和#ifdef #endif的用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

第一种: #if  #else  #endif搭配使用方法:

#define SHOW_LOG 0void main() { #if SHOW_LOGcout << "show log ..." << endl; #elsecout << "not show log..." << endl; #endifcout << "out code here..." << endl;system("pause"); }

如果SHOW_LOG为0,则执行#else后面的代码cout << "not show log..." << endl;

如果SHOW_LOG>0则执行#if SHOW_LOG后面的代码cout << "show log ..." << endl;

外部代码cout << "out code here..." << endl; 因为不在#if和#endif段之间,所以每次都会执行

第二种:#ifdef  #else  #endif搭配使用方法:

#define DEBUGvoid main() { #ifdef DEBUGcout << "define debug..." << endl; #elsecout << "not define debug..." << endl; #endif // DEBUGcout << "out code here..." << endl;system("pause"); }

如果没有#define DEBUG,则执行#else后面的代码cout << "not define debug..." << endl;

如果有#define DEBUG,则执行#ifdef DEBUG后面的代码cout << "define debug..." << endl;

外部代码cout << "out code here..." << endl; 因为不在#if和#endif段之间,所以每次都会执行

 

上述两种方法可以根据个人喜好选择使用,它们的作用就是可以方便我们调试,在不同的设置下可以选择是否输出debug信息或者是否执行某项操作,对调试和代码编写都很方便。

 

总结

以上是生活随笔为你收集整理的C++中 #if #endif 和#ifdef #endif的用法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。