我应该总是使用#if
指令和#endif
吗?
- 如果是 - 如果我只使用
#if
会有什么问题? - 如果没有 - 当我只使用
#if
时,以及当组合#if
/#else
时?
谢谢。
1.If yes - what could be a problem if I only use #if?
正如其他人所提到的,如果您只使用 #if 并且与 #endif 不匹配,预处理器将失败,并显示为编译器错误。
2.If no - why I use only #if,and when combination #if/#else?
即使在 #if/#else 的情况下,最后也应该有一个 #endif。否则它将不起作用。您可以自己尝试并检查。
除了检查和定义宏之外,它们最常用于条件编译。请在下面找到一些相关链接(基本级别):
http://gcc.gnu.org/onlinedocs/cpp/Ifdef.html
http://en.wikipedia.org/wiki/C_preprocessorhttp://www.phanderson.com/C/preprocess.html
如果您没有匹配的#endif
,(或#else
... #endif
) 您的代码将无法编译。
是的。
如果没有#endif
预处理器将无法知道#if
块的结束。