FreeOZ论坛

标题: c code #define MAX(a, b) ((a)>(b)?(a):(b)) [打印本页]

作者: DDD888    时间: 3-8-2016 19:38
标题: 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    时间: 4-8-2016 00:28
如果a,b分别是单个变量,没什么区别,如果是a,b本身是表达式,前者可以避免一些歧义引起的潜在错误。

例子留给楼下。
作者: clarkli    时间: 4-8-2016 01:09
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




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) Powered by Discuz! X3.2