【问题标题】:Does #if #endif act like scope? [duplicate]#if #endif 是否像作用域一样? [复制]
【发布时间】:2014-12-30 12:16:07
【问题描述】:

正如标题所暗示的,我的问题是预处理器指令#if#endif 是否像作用域一样?考虑这个例子:

#define foo 1
#define bar 1

#if foo
#undef bar
#endif //foo

// Is 'bar' defined or undefined here?

【问题讨论】:

  • 如果您的意思是定义的生命周期范围,那么不,它不是一个范围。 bar 将是未定义的。
  • 您想如何检查:if foo,但没有相应的值?
  • 请重新打开。这是一个不同的问题,而不是所引用问题的重复。

标签: c scope c-preprocessor preprocessor-directive


【解决方案1】:

现在用你的例子来测试它,你会看到它:

#include <stdio.h>

#define foo 1
#define bar 1

#if foo
#undef bar
#endif //foo

int main() {

    printf("%d", bar);

    return 0;  
}

我的编译器的输出:

[Error] 'bar' undeclared (first use in this function) 

所以你可以看到它是未声明的

【讨论】:

  • 不管编译器如何,都会这样吗?我在理论上问。
  • @G.Rassovsky 是的(参见:ideone.com/c5qFII
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
相关资源
最近更新 更多