布尔比较说它为真,但if说它为假

Boolean comparison says it is true but the if says it is false

本文关键字:if 布尔 比较      更新时间:2023-10-16

抱歉,如果这个问题对你们来说太基本了,但是我有这个代码让我困惑。我在if语句之前创建了一个布尔比较,并将相同的比较放到if条件中,但它不会在if块中执行。

#include<iostream> #include<cstdio>
using namespace std;
int main()
{
    float s[2] = {0.24};
    printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0) && (s[0] < (1/7 + 1/14));
     printf("nBefore if: s[%d] %fn", 0, s[0]);
     if ((s[0] >= 0) && (s[0] < (1/7 + 1/14)))
     {
        s[0] = 10;
    }
    printf("nAfter if: s[%d] %fn", 0, s[0]);
    return 0;
}
 printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0) && (s[0] < (1/7 + 1/14));

printf("Is this is true? (1 = yes, 0= false): %d", s[0] >= 0 && (s[0] < (1/7 + 1/14)));

先做s[0] >= 0printf再做printf&&