【问题标题】:I'm getting an infinite true bool error when I try to compile this C code and I don't know why当我尝试编译这个 C 代码时,我得到一个无限的 true bool 错误,我不知道为什么
【发布时间】:2021-03-03 12:45:58
【问题描述】:

这是代码

int numberofletters(string input)
{
    int count = 0;
    for(int i = 0, n = strlen(input); i < n; i++)
    {
        if (65 <= (int) input[i] <= 90 | 97 <= (int) input[i] <= 122)
        {
            count += 1;
        }
        else
        {
            count += 0;
        }
    }
    return count;
}

这是我尝试编译时的错误消息

~/pset2/readability/ $ make readability
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    readability.c  -lcrypt -lcs50 -lm -o readability
readability.c:19:34: error: result of comparison of constant 90 with boolean expression is always true [-Werror,-Wtautological-constant-out-of-range-compare]
        if (65 <= (int) input[i] <= 90 | 97 <= (int) input[i] <= 122)
            ~~~~~~~~~~~~~~~~~~~~ ^  ~~
readability.c:19:63: error: result of comparison of constant 122 with boolean expression is always true [-Werror,-Wtautological-constant-out-of-range-compare]
        if (65 <= (int) input[i] <= 90 | 97 <= (int) input[i] <= 122)
                                         ~~~~~~~~~~~~~~~~~~~~ ^  ~~~
2 errors generated.

我不明白为什么这会创建一个无限真实的陈述,我正在创建两个单独的有界空间,并说如果字符串 ascii 存在于任何一个之间,然后做其他事情做其他事情......我错过了什么吗?

【问题讨论】:

  • 逻辑表达式不能像在 C 中那样链接,逻辑 OR 是 || 而不是 | 最后,使用 char 文字而不是幻数。 if (('A' &lt;= input[i] &amp;&amp; input[i] &lt;= 'Z') || ('a' &lt;= input[i] &amp;&amp; input[i] &lt;= 'z').
  • 关于:int numberofletters(string input) string 是如何定义的?注意:string 不是 C 中的有效类型。但是,cs50.h 头文件将 string 定义为计算结果为 char * 的宏。你的程序中是否包含cs50.h
  • 关于:if (65 &lt;= (int) input[i] &lt;= 90 | 97 &lt;= (int) input[i] &lt;= 122) 这非常令人困惑,假设使用的是 ASCII 字符集。最好有#include &lt;ctype.h&gt; 然后声明变为:if( isalpha( input[i] ) )

标签: c if-statement boolean cs50 inequality


【解决方案1】:
65 <= (int) input[i] <= 90

C 不能这样工作。你需要写:

65 <= (int)input[i] && (int)input[i] <= 90

【讨论】:

    【解决方案2】:

    这不是你认为的那样:

    65 <= (int) input[i] <= 90 
    

    此表达式不测试input[i] 是否在 65 和 90 之间。它实际上解析为:

    (65 <= (int) input[i]) <= 90 
    

    所以它首先检查 input[i] 是否大于或等于 65。这将导致值 0 或 1。所以现在你有这个:

    1 <= 90
    

    或者这个:

    0 <= 90
    

    两者都是正确的,这就是您收到该警告的原因。

    您需要分别执行每项检查:

     if ((65 <= input[i] && input[i] <= 90) || (97 <= input[i] && input[i] <= 122))
    

    更好的是,摆脱幻数并使用字符常量:

     if (('A' <= input[i] && input[i] <= 'Z') || ('a' <= input[i] && input[i] <= 'z'))
    

    甚至更好:

    if (isalpha(input[i]))
    

    【讨论】:

      【解决方案3】:

      你有两个问题。

      首先是表达式65 &lt;= (int) input[i] &lt;= 90实际上是(65 &lt;= (int) input[i]) &lt;= 90。这意味着您将65 &lt;= (int) input[i] 的布尔结果与整数90 进行比较。

      而且,由于比较的积分结果总是0 (false) 或1 (true),所以它大于90 的可能性是不存在的。 这就是为什么它说它“总是正确的”。

      第二个问题不大,但您使用按位 OR 运算符| 而不是逻辑 OR 运算符||

      最后,如果要检查字符是否为字母,请改用isalpha。这也可以正确处理非拉丁字母。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多