Isdigit Function In C

Syntax of the isdigit function

The format of the isdigit function in the C programming language is as follows:

Example

int isdigit (int ch);

Parameters:

Ch - It defines the numeric character to be passed in the isdigit function.

Return Value:

The isdigit function examines the 'ch' parameter, and if the supplied character is a digit, it yields a value other than zero. If the character is not a digit, it returns a zero or false Boolean value.

Example 1: Program to check whether the given character is digit or not

Let's develop a program in C that verifies whether the provided characters are digits or not by utilizing the isdigit function.

Example

/* Check whether the given characters are digits or not in C. */
#include <stdio.h>
#include <ctype.h> 

int main ()
{

	// Use the isdigit() function to check the character is digit or not. */
	if (isdigit ( 'P' ) == 0)  //check 'P' is digit 
	{
		printf (" \n The given character 'P' is not digit'. ");
	}
	else
	{
		printf ("\n The given character 'P' is a digit. ");
	}
	
	
	if (isdigit ( '3' ) == 0)  //check for '3'  
	{
		printf (" \n The given character '3' is not digit'. ");
	}
	else
	{
		printf ("\n The given character '3' is a digit. ");
	}

	
	if (isdigit ( '!' ) == 0)  //check for '!' character 
	{
		printf (" \n The given character '!' is not digit'. ");
	}
	else
	{
		printf ("\n The given character '!' is a digit. ");
	}


	if (isdigit ( '$4' ) == 0)  //check for '44'  
	{
		printf (" \n The given character '$4' is not digit'. ");
	}
	else
	{
		printf ("\n The given character '$4' is a digit. ");
	}
	
	
	if (isdigit ( '0' ) == 0)  //check for '0' 
	{
		printf (" \n The given character '0' is not a digit. ");
	}
	else
	{
		printf ("\n The given character '0' is a digit. ");
	}
	
	return 0;
}

Output:

Output

The given character 'P' is not a digit.
The given character '3' is a digit.
The given character '!' is not a digit.
The given character '$4' is not a digit.
The given character '0' is not a digit.

In the given code snippet, various characters such as 'P', '3', '!', '$4', and 0 were specified to verify their validity as digits by employing the isdigit method. Subsequently, the isdigit function is employed to validate and identify that the characters 'P', '1', and $4 are not numerical digits.

Example 2: Program to check whether the character entered by the user is a digit or not

Let's create a program that determines the validity of an input character by utilizing the isdigit function in the C programming language.

Example

/* Check whether the given characters are digits or not in C. */
#include <stdio.h>
#include <ctype.h> 

int main ()
{

	char n; // declare an integer type variable n
	
	printf (" Enter a number to check for valid digits: ");
	scanf ("%c", &n);
	
	// use the isdigit() function to check the digit
	if (isdigit (n))                                              
	{
		printf (" \n It is a digit. ");
	}
	else
	{
		printf (" \n It is not a digit. ");
	}
	return 0;
}

Output:

Output

Enter a number to check for valid digits: 5
It is a digit.

In the program mentioned previously, we accept the character '5' as input from the user and subsequently utilize the isdigit function to verify if the provided argument is a numerical digit. In this case, the character supplied is indeed a digit, resulting in the isdigit function outputting the message "It is a digit."

2 nd execution

Example

Enter a number to check for valid digits: A
It is not a digit.

Similarly, if we input the character 'A' into the isdigit function, the function will verify if 'A' is a valid digit. Since 'A' is not a digit, the function will output the message 'It is not a digit.'

Example 3: Program to print zero and non-zero numbers for passed character in C

Let's create a program that examines each specified character and provides either zero or a non-zero value depending on the characters passed into the isdigit function in the C programming language.

Example

/* create a simple program to return zeroes and non-zeroes values based on the given characters. */
#include <stdio.h>
#include <ctype.h>

int main ()
{
	char num; // declare num as the character type variable
	
	
	num = '0';
	// check character 0 using the isdigit() function
	printf (" \n The isdigit() function returns result based on character (0) passed: %d ", isdigit (num));
	
	num = 'o';
	// check character o using the isdigit() function
	printf (" \n The isdigit() function returns result based on character (o) passed: %d ", isdigit (num));
	
	num = '08';
	// check character 08 using the isdigit() function
	printf (" \n The isdigit() function returns result based on character (08) passed: %d ", isdigit (num));
	
	num = '+';
	// check character + using the isdigit() function
	printf (" \n The isdigit() function returns result based on character (+) passed: %d ", isdigit (num));
	
	num = '%';
	// check character % using the isdigit() function
	printf (" \n The isdigit() function returns result based on character ('%') passed: %d ", isdigit (num));
		
	return 0;
}

Output:

Output

The isdigit() function returns result based on character (0) passed: 1 
The isdigit() function returns result based on character (o) passed: 0
The isdigit() function returns result based on character (08) passed: 1
The isdigit() function returns result based on character (+) passed: 0
The isdigit() function returns result based on character (%) passed: 0

In the program mentioned earlier, we are checking for valid digit characters using the isdigit method. When called, the isdigit function will output 1 for numeric characters and 0 for any alphabetic or special symbols present in the program that do not represent digits.

Example 4: Program to check all the array elements using the isdigit function

Let's create a program that identifies all the legitimate digits within the elements of an array by utilizing the isdigit function in the C programming language.

Example

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

int main ()
{
	int i;
	// declare and initialize the character type array
	char arr[8] = {'E', '08', '@', '-', '3', '/', '007', '$'};
	
	int arr2[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // initialize arr2 with containing all zeroes element
	
	// use for loop to iterate all elements
	for (i = 0; i < 8; i++)
	{
		// check and assign digits to the arr2[i] using the isdigit() function
		arr2[i] = isdigit (arr[i]); 
	}
	
	for (i = 0; i < 8; i++)
	{
	// display array characters
	printf (" \n '%c'", arr[i]);
	
	// check array elements
	if (arr2[i] != 0)
	{
		printf (" is a digit character. ");
	}
	else
	{
		printf (" is not a valid digit character. ");
	}
}
return 0;
}

Output:

Output

'E' is not a valid digit character.
'8' is a digit character.
'@' is not a valid digit character.
'-' is not a valid digit character.
'3' is a digit character.
'/' is not a valid digit character.
'7' is a digit character.
'$' is not a valid digit character.

We have defined and set up the arr variable with certain elements in the previous code. Subsequently, a new array arr2 is generated with zero (0) values to store the outcomes for elements that are not valid digits. The isdigit function is utilized to validate all the elements in the arr array, providing non-zero values for valid digits and zeroes (0) for non-digit elements.

Input Required

This code uses input(). Please provide values below: