#define, #ifdef #undef #endif
我有以下代码
#define PROC_ADD
void main(void)
{
while(1)
{
#ifdef PROC_ADD
// Do this code here then undefined it to run the code in the else
// processing work
#undef PROC_ADD
#else
// now that PROC_ADD has been undefined run this code
// processing work
#endif
}
}
但是,它将运行该代码。但在 PROC_ADD
未定义后,它不会运行 else
中的代码。
我认为原因可能是您只能在编译时定义和取消定义,而不能在运行时定义和取消定义。不过,我不太确定。
I have the following code
#define PROC_ADD
void main(void)
{
while(1)
{
#ifdef PROC_ADD
// Do this code here then undefined it to run the code in the else
// processing work
#undef PROC_ADD
#else
// now that PROC_ADD has been undefined run this code
// processing work
#endif
}
}
However, it will run the code. But it won't run the code in the else
after the PROC_ADD
has been undefined.
I think the reason could be that you can only define and undefine at compile time, and not at run-time. However, I am not really sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您所做的构建时间相当于:
ifdef 等,在构建时发生,但对于您的示例,这不是问题。一旦您评估了if(运行时或构建时形式),就决定采用哪个分支。做出决定后更改某些内容不会改变该决定。
You are doing the build time equivalent of:
ifdef, etc. happen at build time, but for your example, that's not an issue. Once you evaluate the if (either the runtime or build-time form), the decision about which branch to take it made. Changing something after the decision has been made does not change that decision.
#define
仅在预处理期间起作用。因此将按以下方式处理:由于定义了
PROC_ADDR
,预处理器将完全排除#else
分支,然后执行#undef
,因此#else
分支代码永远无法通过预处理,也永远不会到达编译器。#define
s only work during preprocessing. Sowill be processed the following way: since
PROC_ADDR
is defined the preprocessor will completely exclude the#else
branch and then execute#undef
, so the#else
branch code never survives preprocessing and never reaches the compiler.ifdef
条件在预处理器到达时进行评估。当您在ifdef
代码中undef
PROC_ADD
时,预处理器已经决定要包含哪一段代码以及忽略哪一段代码。此外,是的:
ifdef
、undef
等在预处理时进行处理——编译器甚至看不到这些所谓的指令。这当然意味着运行时代码也永远不会看到这些指令。预处理器通过单次遍历文本文件来工作。预处理器甚至不关心您的文本文件恰好包含 C 代码!它对您的
ifdef
和else
以及发生在while
循环内的情况的了解为零。The
ifdef
condition is evaluated when the preprocessor gets to it. When youundef
PROC_ADD
inside theifdef
'd code, the preprocessor has already decided which section of code to include and which to ignore.Furthermore, yes:
ifdef
,undef
, etc are processed at pre-processing time -- the compiler never even sees these so-called directives. This of course means run-time code never sees these directives either.The preprocessor works by taking a single pass through the text file. The preprocessor does not even care that your text file happens to contain C code! It has zero knowledge that your
ifdef
s andelse
s and whatnot happen to be inside awhile
loop.在几乎每种编程语言或语法中,一旦执行进入条件的一个分支(在本例中,条件为#ifdef,即使条件在分支执行期间发生变化,其他分支也会发生变化) 。
我确信您不会期望它打印“Hello”,对吗?
基本上您所说的是
else
分支下的代码应该始终执行,然后就可以了 将其从分支中取出,然后直接将其放入代码中。编译器和执行都只会通过条件语句一次,一旦找到匹配项,它们就不再继续查找。
In just about every programming language or syntax, once execution has entered one branch of a conditional (in this case, the conditional being
#ifdef
, even if the condition changes during execution of the branch, other branches will never be executed.I'm sure you wouldn't expect this to print "Hello", would you?
Basically what you're saying is that the code under the
else
branch should always execute, then just take it out of a branch, and put it directly in the code.Both the compiler and the execution only make one pass through conditionals, once a match has been found they look no further.
如果您希望第二个注释部分表示的代码始终运行,只需执行
如果您希望运行时行为更改,则必须使用运行时构造(例如用作标志的变量) )。正如我们都说的;),预处理器指令仅在编译时评估一次。
If you want the code represented by the second comment section to always run, just do
If you want run-time behaviour changes, you must use run-time constructs (such as a variable to serve as a flag). As we are all saying ;), pre-processor directives are evaluated once only, at compile time.
这样想:即使
x
已在中设置为
部分。false
,以下代码的else
部分也不会执行。 >if在
if(x)
行本身中检查条件。一旦进入该块,它就不会重新计算后续的每个else
部分。编译器已经对此做出了决定。Think about it this way: the
else
portion of the following code is not executed, even thoughx
has been set tofalse
in theif
section.The condition is checked in the
if(x)
line itself. Once it enters that block, it doesn't recalculate each of the subsequentelse
sections. The compiler has already made a decision on that.