Can a C macro definition refer to other macros?

前端 未结 6 1148
既然无缘
既然无缘 2020-12-29 01:01

What I\'m trying to figure out is if something such as this (written in C):

#define FOO 15
#define BAR 23
#define MEH (FOO / BAR)

6条回答
  •  孤城傲影
    2020-12-29 01:49

    Yes, and there is one more advantage of this feature. You can leave some macro undefined and set its value as a name of another macro in the compilation command.

    #define STR "string"
    void main() { printf("value=%s\n", VALUE); }
    

    In the command line you can say that the macro "VALUE" takes value from another macro "STR":

    $ gcc -o test_macro -DVALUE=STR main.c
    $ ./test_macro
    

    Output:

    value=string
    

    This approach works as well for MSC compiler on Windows. I find it very flexible.

提交回复
热议问题