分配以及不等于precedences [英] Assignment and NOT equal precedences

查看:188
本文介绍了分配以及不等于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!=比=

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆