isxdigit()
的函数原型是
int isxdigit( int arg );
它定义在 <ctype.h> 头文件中。
isxdigit() 参数
isxdigit()
函数接受一个字符作为参数。
注意: 在 C 编程中,字符在内部被视为 int
值。
C isxdigit()返回值
如果传递给 isxdigit()
的参数是
- 一个十六进制字符,
isxdigit()
返回一个非零整数。 - 一个非十六进制字符,
isxdigit()
返回 0。
示例 1:C isxdigit() 函数
#include <ctype.h>
#include <stdio.h>
int main() {
char c = '5';
int result;
// hexadecimal character is passed
result = isxdigit(c); // result is non-zero
printf("Result when %c is passed to isxdigit(): %d", c, isxdigit(c));
c = 'M';
// non-hexadecimal character is passed
result = isxdigit(c); // result is 0
printf("\nResult when %c is passed to isxdigit(): %d", c, isxdigit(c));
return 0;
}
输出
Result when 5 is passed to isxdigit(): 128 Result when M is passed to isxdigit(): 0
示例 2:检查十六进制字符的程序
#include <ctype.h>
#include <stdio.h>
int main() {
char c = '5';
printf("Enter a character: ");
c = getchar();
if (isxdigit(c) != 0) {
printf("%c is a hexadecimal character.", c);
} else {
printf("%c is not a hexadecimal character.", c);
}
return 0;
}
输出
Enter a character: f f is a hexadecimal character.