函式
<cctype>

isdigit

int isdigit ( int c );
檢查字元是否為十進位制數字
檢查 c 是否為十進位制數字字元。

十進位制數字是以下任意一個0 1 2 3 4 5 6 7 8 9

有關不同ctype函式對標準 ASCII 字元集中每個字元返回值的詳細圖表,請參閱 <cctype> 標頭檔案的參考。

在 C++ 中,此函式的一個與區域設定相關的模板版本 (isdigit) 存在於標頭檔案 <locale> 中。

引數

c
要檢查的字元,轉型為int型別,或EOF.

返回值

如果 c 確實是空白字元,則返回一個非零值(即true) 如果 c 確實是十進位制數字。否則返回零 (即false)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="1776ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.\n",year,year+1);
  }
  return 0;
}

輸出

The year that followed 1776 was 1777


isdigit用於檢查str中的第一個字元是否為數字,從而判斷其是否為可被 atoi 轉換為整數值的有效候選項。

另見