當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 isdigit用法及代碼示例

C語言ctype頭文件(ctype.h)中isdigit函數的用法及代碼示例。

用法:

int isdigit ( int c );
檢查字符是否為十進製數字
檢查是否c是十進製數字字符。

十進製數字為以下任意值:0 1 2 3 4 5 6 7 8 9

有關不同之處的詳細圖表ctype函數針對標準ASCII字符集的每個字符返回,請參見<cctype>標頭。

在C++中,此函數的locale-specific模板版本(isdigit)存在於標題中<locale>

參數

c
要檢查的字符,強製轉換為int, 或者EOF

返回值

不同於零的值(即,true)如果確實c是一個十進製數字。零(即false) 否則。

示例

/* 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轉換為整數值。

相關用法


注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C isdigit function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。