C Data Type Functions - C isdigit






Determines if a character is a digit (0?9).

Prototype

int isdigit ( int c );

Parameter

This function has the following parameter.

c
Character to be checked, casted to an int, or EOF.

Return

A value different from zero (i.e., true) if indeed c is a decimal digit. Zero (i.e., false) otherwise.

Example


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//from  w  w  w . j av a2  s.c o m
int main (){
  char str[]="123123ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.\n",year,year+1);
  }
  return 0;
} 
       

The code above generates the following result.





Example 2


#include <stdio.h>
#include <ctype.h>
#include <limits.h>
 /*from   w ww  .j ava2 s . c  o m*/
int main(void)
{
    for (int ndx=0; ndx<=UCHAR_MAX; ndx++)
        if (isdigit(ndx)) printf("%c", ndx);
    printf("\n");
}

The code above generates the following result.