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