300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > C++中 #if #endif 和#ifdef #endif的用法

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

时间:2020-03-09 14:28:27

相关推荐

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信息或者是否执行某项操作,对调试和代码编写都很方便。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。