关于c语言#define debug

来源:互联网 发布:centos挂载ntfs硬盘 编辑:程序博客网 时间:2024/06/10 18:50
C/C++ code
#include<iostream>
using namespace std;
#define DEBUGint factorial(int num)
{
if(num == 0)
return 1;
else
{ # if defined(DEBUG)
static i;
cout << "call times:" << ++i << ",num=" << num <<endl;
# endif
return 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这些东西了

原创粉丝点击