Online Compiler C

#include <stdio.h> int is_digit(char c){ if('c'>='0'&&'c'<='9'){ return 1; }else return 0; } int main(void) { int c; int count = 0; while ((c = getchar()) != EOF && c != '.') { if (is_digit((char)c)) { ++count; } } printf("%d", count); return 0; }
这段代码试图统计输入中数字字符的个数,直到遇到句点或文件结束符。但 `is_digit` 函数存在逻辑错误,导致它无法正确判断字符是否为数字。

- 在 `is_digit` 函数中,你写的是 `'c'>='0'` 和 `'c'<='9'`,这里的 `'c'` 是字符常量,而不是传入的参数 `c`。应该使用参数变量 `c` 进行比较,而不是用固定字符 `'c'`。
- 另外,`is_digit` 函数接收的参数类型是 `char`,但你在主函数中传入的是 `(char)c`,这没问题。不过要注意,`getchar()` 返回的是 `int`,直接比较字符时,参数类型转换是合理的,但函数内部必须使用传入的参数值。