分配以及不等于precedences [英] Assignment and NOT equal precedences
问题描述
在页面17 C程序设计语言(第二版)的作者说,
In the page 17 of The C Programming Language, 2nd Edition the writer says that
的 precedence 的!=
比 =
高
所以,当我想测试它,它不是这样的。这是我简单的程序:
So when I want to test it, it's not that way. This is my simple program:
#include <stdio.h>
int main() {
int inp;
while(inp = getchar() != EOF) {
putchar(inp);
}
}
这我尝试读取输入并显示给用户。当我尝试了,没有发生,屏幕是空的,但是当我尝试这一个,一切工作正常:
Which I try to read an input and show it to the user. When I try that, nothing happen and the screen is empty, but when I try this one, everything works fine:
#include <stdio.h>
int main() {
int inp;
while((inp = getchar()) != EOF) {
putchar(inp);
}
}
出了什么事?我已 gcc版本4.2.1(基于苹果公司建立5658)(LLVM建设2336.11.00)
。
推荐答案
发生了什么事是你在说什么,的precedence!=比= $高C $ C>。
What's happening is exactly what you're saying, The precedence of != is higher than that of =
.
这意味着!=
的结合更难的比 =
和
while(inp = getchar() != EOF) {
相当于;
while(inp = (getchar() != EOF)) {
这将 INP
设置为对比,不是你要找的字符值的布尔值。
That will set inp
to the bool value of the comparison, not the character value you're looking for.
这篇关于分配以及不等于precedences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!