温馨提示×

c语言判断字符是否为数字

小亿
138
2024-01-10 13:36:22
栏目: 编程语言

在C语言中,可以使用isdigit()函数来判断一个字符是否为数字。isdigit()函数是C标准库中的一个函数,其原型定义在ctype.h头文件中。

isdigit()函数接受一个字符作为参数,如果该字符是0到9之间的数字字符,则返回非零值(即真),否则返回0(即假)。

下面是一个示例代码:

#include <stdio.h> #include <ctype.h> int main() { char ch = '5'; // 要判断的字符 if (isdigit(ch)) { printf("%c是数字\n", ch); } else { printf("%c不是数字\n", ch); } return 0; } 

输出结果为:

5是数字 

0