2

简述#ifdef、#else、#endif和#ifndef的作用

 2 years ago
source link: https://www.cxyxiaowu.com/17268.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

简述#ifdef、#else、#endif和#ifndef的作用

C++面试题 2月前 0 308

1、利用#ifdef、#endif将某程序功能模块包括进去,以向特定用户提供该功能。在不需要时用户可轻易将其屏蔽。

#ifdef MATH
#include "math.c"
#endif

2、在子程序前加上标记,以便于追踪和调试。

#ifdef DEBUG
printf ("Indebugging......!");
#endif

3、应对硬件的限制。由于一些具体应用环境的硬件不一样,限于条件,本地缺乏这种设备,只能绕过硬件,直接写出预期结果。

注意: 虽然不用条件编译命令而直接用i f 语句也能达到要求, 但那样做目标程序长
( 因为所有语句都编译) , 运行时间长( 因为在程序运行时间对i f 语句进行测试) 。

而采用条件编译, 可以减少被编译的语句, 从而减少目标程序的长度, 减少运行时
间。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK