当我尝试编译此 C 代码时出现无限真实布尔错误,但我不知道为什么
I'm getting an infinite true bool error when I try to compile this C code and I don't know why
这是代码
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.
我不明白为什么这会创建一个无限的 true 语句,我正在创建两个单独的有界空间并说如果字符串 ascii 存在于任何一个之间然后做其他事情做其他事情......我错过了吗有什么?
65 <= (int) input[i] <= 90
C 不是这样工作的。你需要写:
65 <= (int)input[i] && (int)input[i] <= 90
这与您认为的不同:
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]))
你有两个问题。
首先是表达式65 <= (int) input[i] <= 90
实际上是(65 <= (int) input[i]) <= 90
。这意味着您将 65 <= (int) input[i]
的布尔结果与整数 90
.
进行比较
而且,由于比较的积分结果总是 0
(假)或 1
(真),它大于 90
的可能性是, 不存在的。 这就是为什么它声明它“总是正确的”。
第二个问题不大,但是您使用 按位 OR 运算符 |
而不是 logical OR运算符 ||
.
最后,如果要检查字符是否为字母,请改用 isalpha
。这也能正确处理非拉丁字母表。
这是代码
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.
我不明白为什么这会创建一个无限的 true 语句,我正在创建两个单独的有界空间并说如果字符串 ascii 存在于任何一个之间然后做其他事情做其他事情......我错过了吗有什么?
65 <= (int) input[i] <= 90
C 不是这样工作的。你需要写:
65 <= (int)input[i] && (int)input[i] <= 90
这与您认为的不同:
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]))
你有两个问题。
首先是表达式65 <= (int) input[i] <= 90
实际上是(65 <= (int) input[i]) <= 90
。这意味着您将 65 <= (int) input[i]
的布尔结果与整数 90
.
而且,由于比较的积分结果总是 0
(假)或 1
(真),它大于 90
的可能性是, 不存在的。 这就是为什么它声明它“总是正确的”。
第二个问题不大,但是您使用 按位 OR 运算符 |
而不是 logical OR运算符 ||
.
最后,如果要检查字符是否为字母,请改用 isalpha
。这也能正确处理非拉丁字母表。