Determine if a character is a number in the C language.
In C language, the isdigit() function can be used to determine if a character is a digit. This function is included in the C standard library and its prototype is defined in the ctype.h header file.
The isdigit() function takes a character as an argument, and returns a nonzero value (true) if the character is a digit between 0 and 9, otherwise it returns 0 (false).
Here is an example code:
#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;
}
The output result is:
5是数字