C 中的表达式求值
#include <stdio>
int main(){
int x = 4;
int y = 3;
int z;
z = x---y;
printf("%d" , z);
return 0;
}
Linux Mandriva 中的 gcc 编译器将其计算为 (x--)-y
。 我很困惑为什么会这样。 它可能是x - (--y)
。
我知道有些答案会告诉我查看优先级表。我已经经历了所有这些,但疑问仍然存在。
请任何人澄清这一点。
#include <stdio>
int main(){
int x = 4;
int y = 3;
int z;
z = x---y;
printf("%d" , z);
return 0;
}
The gcc compiler in Linux Mandriva evaluates it as (x--)-y
.
I am confused as to why is it so.
It could have been x - (--y)
.
I know some of the answers would tell me to look at precedence tables. Ihave gone through all of them, still the doubt persists.
Please anybody clarify this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C 词法分词器是贪婪的,因此您的表达式会像
应用优先规则之前一样被分词。
The C lexical tokeniser is greedy, so your expression is tokenised as
before precedence rules are applied.
规则是“获取下一个标记时,使用构成有效标记的可能的最长字符序列”。所以
---
是--
后跟-
,而不是相反。优先级实际上与此无关。The rule is "when getting the next token, use the longest sequence of characters possible that constitute a valid token". So
---
is--
followed by a-
and not the other way around. Precedence has actually nothing to do with this.x--
比--x
更强,因此以这种方式编译。后缀比前缀强。请参阅C 运算符优先级表。
x--
is stronger than--x
, so it is compiled this way. Postfix is stronger than prefix.See C Operator Precedence Table.