问题描述
我的代码有两种模式,debug
和 verbose
.我在头文件中将它们定义为,
I have code that I want to have two modes, debug
and verbose
. I define them in my header file as,
#define verbose TRUE
#define debug TRUE
到目前为止,在我的代码中,我一直在使用
In my code so far, I have just been using
#if(debug)
//code
#endif
但是使用起来更合适吗?
but is it more proper to use?
#ifdef debug
// code
#endif
我读过一些关于预处理器宏的文章,但当时并没有什么意义.所以,我有一个问题:#if defined MACRO
是否等同于#ifdef MACRO
?哪个更适合启用/禁用特定代码部分?
I read something about preprocessor macros but it didn't make sense at the time. So, I have a question: Is #if defined MACRO
equivalent to #ifdef MACRO
? and which one is better for enabling/disabling a particular section of code?
推荐答案
#ifdef MACRO
#if defined (MACRO)
会做同样的事情.但是,已定义 (MACRO) 只是一个表达式,在 #if 内部计算为 0 或 1,它可以与其他表达式组合.例如
will do the exact same thing. However, the defined (MACRO) is just an expression that evaluates to 0 or 1 inside the #if, and it can be combined with other expressions. For example
#if defined (MACRO) && ! defined (MACRO2)
// Do this
#else
// Do that
#endif
尝试使用#ifdef 执行此操作 - 除非您的代码变得非常笨拙,否则您不能这样做.
Try doing that with #ifdef - you can't unless your code gets really clumsy.
这篇关于#if defined MACRO 是否等同于#ifdef MACRO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!