TL;博士; 阅读运算符优先级。
+
绑定高于==
哪个绑定高于|
。
预处理后,您的printf()
语句看起来像
printf("%d",2|0==2|0+2);
这与
printf("%d",2|(0==2)|(0+2));
这是
printf("%d",2|0|2);
忠告:不要在真实场景中编写此类代码。启用最低级别的编译器警告后,您的代码会生成
source_file.c: In function ‘main’:
source_file.c:4:12: warning: suggest parentheses around comparison in operand of ‘|’ [-Wparentheses]
#define x 2|0
^
source_file.c:8:21: note: in expansion of macro ‘x’
printf("%d\n\n",x==x+2);
^
source_file.c:4:12: warning: suggest parentheses around arithmetic in operand of ‘|’ [-Wparentheses]
#define x 2|0
^
source_file.c:8:24: note: in expansion of macro ‘x’
printf("%d\n\n",x==x+2);
因此,当您将 MACRO 定义更改为理智的时候,例如
#define x (2|0)
结果也将改变,因为括号将保证显式优先级。