- C/C++ code
-
#include<iostream>using namespace std;#define DEBUG int factorial(int num){if(num == 0)return 1;else{ # if defined(DEBUG)static i;cout << "call times:" << ++i << ",num=" << num <<endl;# endifreturn factorial(num -1) * num;} } int main(){int number;cout << "enter a integer:";cin >> number;cout << "the factorial number:"<< factorial(number)<< endl; return 0;}
其中#define DEBUG 该怎么理解 还有
什么时候 define(DEBUG)就为真了。
答:这是常用的一个小技巧
在调试代码的阶段,保留#define DEBUG这一行,后面#if defined(DEBUG)部分的代码就可以起作用,打印出一些有助于调试的信息。
等到出release版的时候,就可以把#define DEBUG这一行删掉,或者改成#undef DEBUG,后面那段代码就不会被编译,也就不会打印call times这些东西了