Strange behavior of Macro-expansion

前端 未结 3 1119
悲&欢浪女
悲&欢浪女 2020-12-12 01:47

Here\'s the code:

#include 
#include 
#define VAL1(a,b)    a*b
#define VAL2(a,b)    a/b
#define VAL3(a,b)    ++a%b
int main()
{         


        
3条回答
  •  独厮守ぢ
    2020-12-12 02:21

    If you really think it is necessary to use such macros, you'll need to use parenthesis () to avoid side-effects:

    #define VAL1(a,b)    ((a)*(b))
    #define VAL2(a,b)    ((a)/(b))
    #define VAL3(a,b)    ((++a)%(b))
    

提交回复
热议问题