2014-11-21 65 views
2

我有具有如下形式的一个现有的预处理条件指令一些代码:下预处理器条件指令优先级和嵌套

#ifndef SYMBOL_XYZ 
// some code here 
#else 
// some other code here 
#endif 

和我要添加取代了逻辑新的条件,我想这是做到这一点的方法,但是当涉及到C预处理器时,我不确定嵌套和优先级的细微之处。

#ifdef NEW_SYMBOL_ABC 
// some new code here that takes precedence over the other two conditions 
#else 
    #ifndef SYMBOL_XYZ 
    // some code here 
    #else 
    // some other code here 
    #endif 
#endif 

我有这样的权利吗?它会等同于:

#ifndef NEW_SYMBOL_ABC 
    #ifndef SYMBOL_XYZ 
    // some code here 
    #else 
    // some other code here 
    #endif 
#else 
    // some new code here that takes precedence over the other two conditions 
#endif 
+0

它被称为预处理器 – 2014-11-21 04:15:24

+0

看起来很适合我。 – JS1 2014-11-21 04:18:55

+0

没关系。 如果你检查gcc头文件,你会发现很多像这样的预处理器嵌套。 – sunny1304 2014-11-21 04:20:45

回答

2

试试这个..

#ifdef NEW_SYMBOL_ABC 
// some new code here that takes precedence over the other two conditions 
#elif !defined(SYMBOL_XYZ) 
// some code here 
#else 
// some other code here 
#endif 

以上是我常用的,应该可以和gcc一起工作。 不确定,但应该使用visual C++和其他编译器。

+0

是的,这是标准和便携式。而且,值得一提的是,它等同于问题中的代码,这也是正确的。 – Potatoswatter 2014-11-21 04:26:07