C library function - isdigit()



Description

The C library function int isdigit(int c) checks if the passed character is a decimal digit character.

Decimal digits are (numbers) − 0 1 2 3 4 5 6 7 8 9.

Declaration

Following is the declaration for isdigit() function.

int isdigit(int c);

Parameters

  • c − This is the character to be checked.

Return Value

This function returns non-zero value if c is a digit, else it returns 0.

Example

The following example shows the usage of isdigit() function.

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

int main () {
   int var1 = 'h';
   int var2 = '2';
    
   if( isdigit(var1) ) {
      printf("var1 = |%c| is a digit\n", var1 );
   } else {
      printf("var1 = |%c| is not a digit\n", var1 );
   }
   
   if( isdigit(var2) ) {
      printf("var2 = |%c| is a digit\n", var2 );
   } else {
      printf("var2 = |%c| is not a digit\n", var2 );
   }
   
   return(0);
}

Let us compile and run the above program, to produce the following result −

var1 = |h| is not a digit
var2 = |2| is a digit
ctype_h.htm
Advertisements