isalpha() and isdigit() in C/C++



Both isalpha() and isdigit() are the part of C/C++ library function which can be used to check whether a given character is an alphabetical letter or a digit, respectively. These fuctions accept the given character/digit as input and determine the presence of letter or digit in correct/incorrect form.

What is isalpha() in C/C++?

The isalpha() function is used to check that a character is an alphabet or not. This function is declared in ctype.h header file. It returns an integer value, if the argument is an alphabet otherwise, it returns zero.

Syntax

Following is the basic syntax of isalpha() in C/C++ language,

int isalpha(int value);

Here,

  • value : This is a single argument of integer type.

Example

C C++
#include<stdio.h>
#include<ctype.h>

int main() {
   char val1 = 's';
   char val2 = '8';

   if(isalpha(val1))
   printf("The character is an alphabet\n");
   else
   printf("The character is not an alphabet\n");

   if(isalpha(val2))
   printf("The character is an alphabet\n");
   else
   printf("The character is not an alphabet");

   return 0;
}

Output

The above program produces the following result:

The character is an alphabet
The character is not an alphabet
#include<iostream>
#include<cctype>
using namespace std;

int main() {
   char val1 = 's';
   char val2 = '8';

   if (isalpha(val1))
       cout << "The character is an alphabet\n";
   else
       cout << "The character is not an alphabet\n";

   if (isalpha(val2))
       cout << "The character is an alphabet\n";
   else
       cout << "The character is not an alphabet\n";

   return 0;
}

Output

The above program produces the following result:

The character is an alphabet
The character is not an alphabet

What is isdigit() in C/C++?

The isdigit() function is used to check that character is a numeric character or not. This function is declared in "ctype.h" header file. It returns an integer value, if the argument is a digit otherwise, it returns zero.

Syntax

Here is the basic syntax of isdigit() in C/C++ language,

int isdigit(int value);

Here,

  • value : This is a single argument of integer type.

Example

C C++
#include<stdio.h>
#include<ctype.h>

int main() {
   char val1 = 's';
   char val2 = '8';

   if(isdigit(val1))
   printf("The character is a digit\n");
   else
   printf("The character is not a digit\n");

   if(isdigit(val2))
   printf("The character is a digit\n");
   else
   printf("The character is not a digit");

   return 0;
}

Output

The above program produces the following result:

The character is not a digit
The character is a digit
#include<iostream>
#include<cctype>
using namespace std;

int main() {
   char val1 = 's';
   char val2 = '8';

   if (isdigit(val1))
       cout << "The character is a digit\n";
   else
       cout << "The character is not a digit\n";

   if (isdigit(val2))
       cout << "The character is a digit\n";
   else
       cout << "The character is not a digit\n";

   return 0;
}

Output

The above program produces the following result:

The character is not a digit
The character is a digit

Difference between isalpha() and isdigit()

Below is the tabular differences of isalpha() anf isdigit() in C/C++ programming:

Function Definition Header File Return Value Example
isalpha() This check whether a character is an aplhabet (A-Z or a-z) <cctype> in C++, <ctype.h> in C For Non-zero its true, if the character is an alphabet, otherwise false. isalpha('A') = true, isalpha('5') = false
isdigit() This check whether a character is a digit (0-9) <cctype> in C++, <ctype.h> in C For Non-zero its true, if the character is a digit, otherwise false. isdigit('8') = true, isdigit('B') = false
Updated on: 2025-06-09T14:48:51+05:30

38K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements