在C语言中执行#if条件的其他方法。

问题描述 投票:0回答:1

我试图执行一个字符串变量 "str "中给出的条件。我试着把它放在一个宏定义里面,但它不工作。不像在 "CONDITION_DEF "中直接定义条件。


    #define CONDITION_STR str
    #define CONDITION_DEF defined A && defined B
    #define A 1
    #define B 1 
    int main(){
        char *str = "defined A && defined B";
        #if CONDITION_STR
        printf("Condition from str: A and B are defined");
        #endif 
        #if CONDITION_DEF
        printf("Condition from define: A and B are defined");
        #endif
     }
    

OUTPUT。

"Condition from define: A and B are defined"

条件被放置在一个字符串变量中,这样我就可以在运行时改变它。有没有其他方法可以从字符串变量中执行#if条件?谢谢!我正在尝试执行给定的条件。

c c-preprocessor
1个回答
4
投票

这是不可能的,因为预处理是在实际编译C代码之前完成的。预处理程序对C和C变量一无所知。

© www.soinside.com 2019 - 2024. All rights reserved.