Can a C macro definition refer to other macros?

前端 未结 6 1146
既然无缘
既然无缘 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:36

    Yes, that is supported. And used quite a lot!

    One important thing to note though is to make sure you paranthesize the expression otherwise you might run into nasty issues!

    #define MEH FOO/BAR
    
    // vs
    
    #define MEH (FOO / BAR)
    
    // the first could be expanded in an expression like 5 * MEH to mean something 
    //   completely different than the second
    

提交回复
热议问题