Printing name and value of a macro

前端 未结 11 2637
别跟我提以往
别跟我提以往 2020-12-28 21:46

I have a C program with a lot of optimizations that can be enabled or disabled with #defines. When I run my program, I would like to know what macros have been

11条回答
  •  [愿得一人]
    2020-12-28 22:09

    As long as you are willing to put up with the fact that SOMESTRING=SOMESTRING indicates that SOMESTRING has not been defined (view it as the token has not been redefined!?!), then the following should do:

    #include 
    
    #define STR(x)   #x
    #define SHOW_DEFINE(x) printf("%s=%s\n", #x, STR(x))
    
    #define CHARLIE -6
    #define FRED 1
    #define HARRY FRED
    #define NORBERT ON_HOLIDAY
    #define WALLY
    
    int main()
    {
        SHOW_DEFINE(BERT);
        SHOW_DEFINE(CHARLIE);
        SHOW_DEFINE(FRED);
        SHOW_DEFINE(HARRY);
        SHOW_DEFINE(NORBERT);
        SHOW_DEFINE(WALLY);
    
    return 0;
    }
    

    The output is:

    BERT=BERT
    CHARLIE=-6
    FRED=1
    HARRY=1
    NORBERT=ON_HOLIDAY
    WALLY=
    

提交回复
热议问题