<分区>
//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 中的字符串操作的更多信息。每当我运行此代码时,除非我将字母或符号传递到 check_num,否则程序会输出“0”而不是像我希望的那样输出“1”。这是怎么回事?