Easy Tutorial
❮ C Function Ispunct C Exercise Example41 ❯

C Library Function - isdigit()

C Standard Library - <ctype.h>

Description

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

Decimal digits are: 0 1 2 3 4 5 6 7 8 9

Declaration

Here is the declaration for the isdigit() function.

int isdigit(int c);

Parameters

Return Value

The function returns a non-zero value if c is a digit, otherwise it returns 0.

Example

The following example demonstrates the use of the 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's compile and run the above program, which will produce the following result:

var1 = |h| is not a digit
var2 = |2| is a digit

C Standard Library - <ctype.h>

❮ C Function Ispunct C Exercise Example41 ❯