2014-11-21 59 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