Closed. This question is off-topic. It is not currently accepting answers. Learn more。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
三年前关闭。
我正在尝试学习更多关于C中字符串操作的知识。每当我运行此代码时,程序将输出“0”,而不是像我希望的那样输出“1”,除非我将字母或符号传递到check-num中。这是怎么回事?
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
三年前关闭。
//This program determines if a string is made up entirely of numbers.
//It should output 1 if the string only consists of numbers and either
//output 0 or nothing at all if otherwise.
#include <stdio.h>
#include <stdbool.h>
bool check_num(const char *str) {
for (int i = 0; str[i] != '\0'; ++i){ //Iterating through
if (str[i] < '0' || str[i] > '0'){
return false; //Is this return statement correct?
}
}
return true;
}
int main() {
bool a = check_num("1");
printf("%d\n", a);
return 0;
}
我正在尝试学习更多关于C中字符串操作的知识。每当我运行此代码时,程序将输出“0”,而不是像我希望的那样输出“1”,除非我将字母或符号传递到check-num中。这是怎么回事?
最佳答案
那应该是
if (str[i] < '0' || str[i] > '9') {
// ^
关于c - 在C中显示正确的 bool 值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37466748/