Javatpoint Logo
Javatpoint Logo

isdigit() function in C

This topic will discuss the isdigit() function in the C language. The isdigit() function is a predefined function of the C library, which is used to check whether the character is a numeric character from 0 to 9 or not. And if the given character is a numeric number or digit, it returns a true Boolean value or non-zero; else, it returns zero or false value. The isdigit function is declared inside the ctype.h header file, so we must add to the C program.

For example, suppose we enter character 'h' to the isdigit() function; the function checks whether the input character is a digit or not. Here the character is not a digit. So the isdigit() function returns zero (0) or false value. Similarly, we again enter numeric character '5' to the isdigit() function. This time, the function returns a true or non-zero value because '5' is a numeric characte from 0 to 9 digits.

Syntax of the isdigit() function

Following is the syntax of the isdigit() function in the C language.

Parameters:

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

Return Value:

The isdigit() function checks the 'ch' argument, and if the passed character is a digit, it returns a non-zero value. Otherwise, it shows a zero or false Boolean value.

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

Let's create a program to check the given characters are digit or not using the isdigit() function in the C programming.

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 above program, we defined different characters like 'P', '3', '!', '$4', 0 to check whether these are valid characters or not using the isdigit() function. And then, we use the isdigit() function that checks and returns the characters 'P', '1', $4 are not digits.

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

Let's write a program to find whether the input character is valid or not using the isdigit() function in C.

Output:

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

In the above program, we input the '5' character from the user and then use the isdigit function to check whether the passed argument is a digit. Here, the passed character is a digit, so the isdigit() function returns the statement "It is a digit."

2nd execution

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

Similarly, when we enter character 'A' to the isdigit() function, the function checks for the valid character, and we can see character 'A' is not a digit. So, the function returns the statement 'It is not a digit.'

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

Let's write a program to check all given characters and returns zeroes and non-zeroes values based on the passed characters to the isdigit() function in C.

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 above program, we tests for valid given digit characters using the isdigit() function. The isdigit() function returns 1 to the numeric characters and 0 to the alphabetic or special symbols defined in the program that are not digits.

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

Let's write a program to find all the valid digits of the array element using the isdigit() function in C.

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 declared and initialized the arr[] variable with some elements in the above program. And then, we create another array arr2[] that contains zero (0) to assign the result to those elements that are not valid digits. The isdigit() function checks all the arr[] array elements and returns non-zero values to the valid digit elements. Else it returns zeroes (0) to non-digit elements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA