gpt4 book ai didi

c - 在 C 中显示正确的 bool 值

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:10 25 4
gpt4 key购买 nike

<分区>

 //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”。这是怎么回事?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com