C预处理器条件指令优先级和嵌套

C预处理器条件指令优先级和嵌套,c,c-preprocessor,C,C Preprocessor,我有一些代码,其中包含一个现有的预处理器条件指令: #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

我有一些代码,其中包含一个现有的预处理器条件指令:

#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
试试这个

#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++和其他编译器一起工作。 它被称为预处理器,没问题。如果你检查gcc头文件,你会发现很多像这样的预处理器嵌套。是的,这是标准的和可移植的。值得一提的是,它相当于问题中的代码,这也是正确的。