DDD888 发表于 3-8-2016 18:38:56

c code #define MAX(a, b) ((a)>(b)?(a):(b))

I forgot what is the advantage of this macro #define MAX(a, b) ((a)>(b)?(a):(b)) vs #define MAX(a, b) (a>b?a:b)

Thanks in advance

ubuntuhk 发表于 3-8-2016 23:28:46

如果a,b分别是单个变量,没什么区别,如果是a,b本身是表达式,前者可以避免一些歧义引起的潜在错误。

例子留给楼下。

clarkli 发表于 4-8-2016 00:09:45

In this case it might be fine without the extra parentheses because precedence of ternary operator is after arithmetic operators.
http://en.cppreference.com/w/c/language/operator_precedence
页: [1]
查看完整版本: c code #define MAX(a, b) ((a)>(b)?(a):(b))