#define, #ifdef #undef #endif

发布于 2024-08-12 10:20:51 字数 441 浏览 4 评论 0原文

我有以下代码

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烟花易冷人易散 2024-08-19 10:20:51

您所做的构建时间相当于:

int x = 1;

int main()
{
    if (x)
    {
        ...
        x = 0;
    }
    else
    {
        ...
    }
}

ifdef 等,在构建时发生,但对于您的示例,这不是问题。一旦您评估了if(运行时或构建时形式),就决定采用哪个分支。做出决定后更改某些内容不会改变该决定。

You are doing the build time equivalent of:

int x = 1;

int main()
{
    if (x)
    {
        ...
        x = 0;
    }
    else
    {
        ...
    }
}

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.

剩一世无双 2024-08-19 10:20:51

#define仅在预处理期间起作用。因此

#define PROC_ADD 
void main(void) 
{
#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_ADDR,预处理器将完全排除#else分支,然后执行#undef,因此#else 分支代码永远无法通过预处理,也永远不会到达编译器。

#defines only work during preprocessing. So

#define PROC_ADD 
void main(void) 
{
#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 
}

will 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.

笑脸一如从前 2024-08-19 10:20:51

ifdef 条件在预处理器到达时进行评估。当您在 ifdef 代码中 undef PROC_ADD 时,预处理器已经决定要包含哪一段代码以及忽略哪一段代码。

此外,是的:ifdefundef等在预处理时进行处理——编译器甚至看不到这些所谓的指令。这当然意味着运行时代码也永远不会看到这些指令。

预处理器通过单次遍历文本文件来工作。预处理器甚至不关心您的文本文件恰好包含 C 代码!它对您的 ifdefelse 以及发生在 while 循环内的情况的了解为零。

The ifdef condition is evaluated when the preprocessor gets to it. When you undef PROC_ADD inside the ifdef'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 ifdefs and elses and whatnot happen to be inside a while loop.

﹏雨一样淡蓝的深情 2024-08-19 10:20:51

在几乎每种编程语言或语法中,一旦执行进入条件的一个分支(在本例中,条件为#ifdef,即使条件在分支执行期间发生变化,其他分支也会发生变化) 。

我确信您不会期望它打印“Hello”,对吗?

if (i == 1)
    i = 0;
else
    printf("Hello\n");

基本上您所说的是 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?

if (i == 1)
    i = 0;
else
    printf("Hello\n");

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.

哽咽笑 2024-08-19 10:20:51

如果您希望第二个注释部分表示的代码始终运行,只需执行

#ifdef PROC_ADD
// Do the stuff to be done if PROC_ADD is defined
#undef PROC_ADD
#endif
// Do the stuff to always be done

如果您希望运行时行为更改,则必须使用运行时构造(例如用作标志的变量) )。正如我们都说的;),预处理器指令仅在编译时评估一次。

If you want the code represented by the second comment section to always run, just do

#ifdef PROC_ADD
// Do the stuff to be done if PROC_ADD is defined
#undef PROC_ADD
#endif
// Do the stuff to always be done

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.

此岸叶落 2024-08-19 10:20:51

这样想:即使 x 已在 中设置为 false,以下代码的 else 部分也不会执行。 >if 部分。

if(x) 行本身中检查条件。一旦进入该块,它就不会重新计算后续的每个 else 部分。编译器已经对此做出了决定。

bool x = true;
if(x)
{
  // Do something
  x = false;
}
else
{
  // Else code
}

Think about it this way: the else portion of the following code is not executed, even though x has been set to false in the if section.

The condition is checked in the if(x) line itself. Once it enters that block, it doesn't recalculate each of the subsequent else sections. The compiler has already made a decision on that.

bool x = true;
if(x)
{
  // Do something
  x = false;
}
else
{
  // Else code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文