相关疑难解决方法(0)

处于相同"if"条件的两个"=="相等运算符未按预期工作

我试图建立三个相等变量的相等,但下面的代码不打印应该打印的明显正确的答案.有人可以解释,编译器如何解析if(condition)内部给定的?

#include<stdio.h>
int main()
{
        int i = 123, j = 123, k = 123;
        if ( i == j == k)
                printf("Equal\n");
        else
                printf("NOT Equal\n");
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

manav@workstation:~$ gcc -Wall -pedantic calc.c
calc.c: In function ‘main’:
calc.c:5: warning: suggest parentheses around comparison in operand of ‘==’
manav@workstation:~$ ./a.out
NOT Equal
manav@workstation:~$
Run Code Online (Sandbox Code Playgroud)

编辑:

通过下面给出的答案,以下声明可以检查上面的相等吗?

if ( (i==j) == (j==k))
Run Code Online (Sandbox Code Playgroud)

c c++ compiler-construction operators equals-operator

11
推荐指数
4
解决办法
3513
查看次数

链接多个大于/小于运算符

在一个if声明中我想要包括一个范围,例如:

if(10 < a < 0)
Run Code Online (Sandbox Code Playgroud)

但通过这样做,我得到一个警告"毫无意义的比较".但是,这没有任何警告,工作正常:

if(a<10 && a>0)
Run Code Online (Sandbox Code Playgroud)

第一种情况可以在C中实现吗?

c

7
推荐指数
3
解决办法
3万
查看次数

比较运算符的类似数学的链接 - 例如,"if((5 <j <= 1))"

int j=42;
if( (5<j<=1) ) {
    printf("yes");
} else {
    printf("no");
}
Run Code Online (Sandbox Code Playgroud)

输出:

yes
Run Code Online (Sandbox Code Playgroud)

为什么输出是?
不是条件只有一半是真的吗?

c c++ if-statement comparison-operators

0
推荐指数
3
解决办法
363
查看次数