isdigit() function in C/C++ with Examples
The isdigit() in C is a function that can be used to check if the passed character is a digit or not. It returns a non-zero value if it’s a digit else it returns 0. For example, it returns a non-zero value for ‘0’ to ‘9’ and zero for others.
The isdigit() function is declared inside ctype.h header file.
C isdigit() Syntax
isdigit(int arg);
C isdigit() Parameters
This function takes a single argument in the form of an integer and returns the value of type int.
Note: Even though isdigit() takes an integer as an argument, the character is passed to the function. Internally, the character is converted to its ASCII value for the check.
C isdigit() Return Value
This function returns an integer value on the basis of the argument passed to it
- If the argument is a numeric character then it returns a non-zero value(true value).
- It returns zero(false value) if the argument is a non-numeric character.
Example: C Program to check whether the character is a digit or not using isdigit() Function
C
// C program to demonstrate isdigit() #include <ctype.h> #include <stdio.h> // Driver Code int main() { // Taking input char ch = '6' ; // Check if the given input // is numeric or not if ( isdigit (ch)) printf ( "Entered character is" " numeric character" ); else printf ( "Entered character is not" " a numeric character" ); return 0; } |
Output
Entered character is numeric character
Working of isdigit() function in C
The working of the isdigit() function is as follows:
- STEP 1: The isdigit() function takes the character to be tested as the argument.
- STEP 2: The ASCII value of the character is checked.
- STEP 3A: If the ASCII value of the character is between 48 ( i.e ‘0’ ) and 57 ( i.e. ‘9’ ), a non-zero value (TRUE) is returned.
- STEP 3B: If the ASCII value of the character is not between 48 ( i.e ‘0’ ) and 57 ( i.e. ‘9’ ), Zero value (FALSE) is returned.
Please Login to comment...