isdigit() function in C/C++ with Examples
The isdigit(c) is a function in C which 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() is declared inside ctype.h header file.
- It is used to check whether the entered character is a numeric character[0 – 9] or not.
- It takes a single argument in the form of an integer and returns the value of type int.
- 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.
Header File:
#include <ctype.h>
Syntax:
std::isdigit(int arg)
Parameter: The template std::isdigit() accepts a single parameter of integer type.
Return type: 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), otherwise it returns zero(false value).
Below is the program to illustrate the same:
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 ( "\nEntered character is" " numeric character" ); else printf ( "\nEntered character is not" " a numeric character" ); return 0; } |
// C++ program to demonstrate isdigit()
#include <ctype.h>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Taking input
char ch = ‘6’;
// Check if the given input
// is numeric or not
if (isdigit(ch))
cout << "\nEntered character is"
<< " numeric character";
else
cout << "\nEntered character is not"
" a numeric character";
return 0;
}
Entered character is numeric character
Please Login to comment...