A> = B> = C运营商C 2 [英] A>=B>=C operators in C?

查看:211
本文介绍了A> = B> = C运营商C 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

#include<stdio.h>

int main() {
    if (2 >= 1 >= 1)
        printf("1\n");
    if (200 >= 100 >= 100)
        printf("2\n");
    return 0;
}

纽约纽约@:〜/ PRG $ GCC sample.c文件

nyc@nyc:~/PRG$ gcc sample.c

纽约纽约@:〜/ PRG $ ./a.out 1

nyc@nyc:~/PRG$ ./a.out 1

为什么这个方案只打印1即使以第二次的前pression值为TRUE?

Why does this program print only 1 even if the second expression evaluates to TRUE?

推荐答案

C不支持链接这样的运营商。你必须除以前pression一分为二,由逻辑运算符分隔的:

C doesn't support chaining operators like that. You'll have to divide that expression into two, seperated by the logical AND operator:

if(2 >= 1 && 1 >= 1)
if(200 >= 100 && 100 >= 100)

,否则它是作为执行

otherwise, it is executed as

if((2 >= 1) >= 1)
if((200 >= 100) >= 100)

和左侧部分将执行第一,将评估为1,如果该条件为真,将计算为0,如果是假的,因此,上述条件变得

and the left part will execute first and will evaluate to 1 if that condition is true and will evaluate to 0, if it is false and so, the above conditions becomes

if(1 >= 1) /* which is true */
if(1 >= 100) /* which is false */

这篇关于A&GT; = B&GT; = C运营商C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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