C isdigit()

isdigit()函数原型

int isdigit( int arg );

isdigit()函数接受一个整数作为参数,并返回一个int类型的值。

尽管isdigit()接受整数作为参数,但传递给函数的是字符。在内部,该字符将被转换为其ASCII值进行检查。

它定义在 <ctype.h> 头文件中。


C isdigit()返回值

返回值 备注
非零整数 ( x > 0 ) 参数是数字字符。
零 (0) 参数不是数字字符。

示例:C isdigit()函数

#include <stdio.h>
#include <ctype.h>

int main()
{
    char c;
    c='5';
    printf("Result when numeric character is passed: %d", isdigit(c));

    c='+';
    printf("\nResult when non-numeric character is passed: %d", isdigit(c));

    return 0;
}

输出

Result when numeric character is passed: 2048
Result when non-numeric character is passed: 0

示例:C 程序检查用户输入的字符是否为数字字符

#include <stdio.h>
#include <ctype.h>

int main()
{
    char c;

    printf("Enter a character: ");
    scanf("%c",&c);

    if (isdigit(c) == 0)
         printf("%c is not a digit.",c);
    else
         printf("%c is a digit.",c);
    return 0;
}

输出

Enter a character: 8
8 is a digit.
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战