为什么第二个 printf 的输出是: max of 50 and 67 is 62 ?为什么 50 和 62 的最大值不是 57?
#define MAX(a,b) ((a)>(b) ? (a): (b))
int incr(){
static int i =42;
i += 5;
return i;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x = 50;
printf("max of %d and %d is %d\n",x, incr(), MAX(x, incr()));
printf("max of %d and %d is %d",x, incr(), MAX(x, incr()));
return 0;
}
printf("max of %d and %d is %d\n",x, incr(), MAX(x, incr()));
宏替换后,变为:
printf("max of %d and %d is %d\n",x, incr(), ((x)>(incr()) ? (x): (incr())));
// ^1 ^2 ^3
incr()
在此单个函数调用中被多次调用,未指定首先评估哪个参数。无论是先调用第一个还是第二个都会导致结果出乎意料。
唯一可以确定的是由于短路?:
, (x)>(incr()
求值以确定表达式的值是否为(x)
或第三个的值incr()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)