ifdef endif到底是干啥的啊
答案:3 悬赏:20
解决时间 2021-01-27 00:37
- 提问者网友:趣果有间
- 2021-01-26 14:18
ifdef endif到底是干啥的啊
最佳答案
- 二级知识专家网友:醉吻情书
- 2021-01-26 15:10
"#ifdef 语句1
程序2
#endif“
可翻译为:如果宏定义了语句1则程序2。
作用:我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。
代码举例:新建define.cpp文件
#include "iostream.h"
int main()
{ #ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:
Press any key to continue
改写代码如下:
#include "iostream.h"
#define DEBUG
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:
Beginning execution of main()
Press any key to continue
更一般的情况是,#define语句是包含在一个特定的头文件中。
比如,新建头文件head.h,在文件中加入代码:
#define DEBUG
#ifdef DEBUG
#endif
而在define.cpp源文件中,代码修改如下:
#include "iostream.h"
#include "head.h"
#define DEBUG
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果如下:
Beginning execution of main()
Press any key to continue
结论:
通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
程序2
#endif“
可翻译为:如果宏定义了语句1则程序2。
作用:我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。
代码举例:新建define.cpp文件
#include "iostream.h"
int main()
{ #ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:
Press any key to continue
改写代码如下:
#include "iostream.h"
#define DEBUG
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果为:
Beginning execution of main()
Press any key to continue
更一般的情况是,#define语句是包含在一个特定的头文件中。
比如,新建头文件head.h,在文件中加入代码:
#define DEBUG
#ifdef DEBUG
#endif
而在define.cpp源文件中,代码修改如下:
#include "iostream.h"
#include "head.h"
#define DEBUG
int main()
{
#ifdef DEBUG
cout<< "Beginning execution of main()";
#endif
return 0;
}
运行结果如下:
Beginning execution of main()
Press any key to continue
结论:
通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
全部回答
- 1楼网友:十年萤火照君眠
- 2021-01-26 16:24
如果你定义了某个东西,则执行一段代码,这段代码是包含在ifdef到endif之间的
- 2楼网友:杯酒困英雄
- 2021-01-26 15:51
这个是C中的。意思是说如果你定义了某个东西,则执行一段代码,这段代码是包含在ifdef到endif之间的。比如,你debug一个程序,但是到最后你需要将debug的代码删掉,很多则很麻烦。但是如果你那些debug的测试程序都是包含在ifdef和endif中的,便只需要将DEBUG注释掉就行了。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯