Skip to content
Related Articles

Related Articles

isdigit() function in C/C++ with Examples
  • Last Updated : 19 Jan, 2021

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


// C++ program to demonstrate isdigit()
#include
#include
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; } [tabbyending]

Output:

Entered character is numeric character

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :