C library function isalnum( )

In this tutorial, you will learn about C library function isalnum()  which is a character handling function  used to check whether the character supplied as the argument is an alphabet or a number.
C library function isalnum

We should include ctype.h header to use isalnum( ) function.

#include <ctype.h>

C library function isalnum( ): Function prototype


int isalnum( int ch);

This function checks whether its argument is a digit (0-9)  or letter (a-z)/(A-Z). Letter may be an uppercase or a lowercase.

It returns a true value if the argument is a digit (0-9) or letter (a-z)/(A-Z) and zero (0) otherwise.

Example: Demonstration of isalnum( ) function

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

int main()
{
   printf("Demonstration of isalnum():\n");
   printf("\n%s%s", isalnum('A') ? "A is a " : "A is not a ", "digit or a letter");
   printf("\n%s%s", isalnum('c') ? "c is a " : "c is not a ", "digit or a letter");
   printf("\n%s%s", isalnum('!') ? "! is a " : "! is not a ", "digit or a letter");
   printf("\n%s%s", isalnum('2') ? "2 is a " : "2 is not a ", "digit or a letter");

   return 0;
}

Output

Demonstration of isalnum():

A is a digit or a letter
c is a digit or a letter
! is not a digit or a letter
2 is a digit or a letter

[adsense1]

Explanation
In the above example, conditional operator ?: also known as a ternary operator is used to determine which string should be printed i.e. “is a” or “is not a”.

isalnum test whether the argument is digit/letter or not and returns true or false value which is the test condition of a conditional operator.

This can be simply achieved by if...else condition.

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

int main()
{
   printf("Demonstration of isalnum( ):\n");

   if( isalnum('A') == 0)
     printf("\nA is not a digit or a letter");
   else
     printf("\nA is a digit or a letter");

   if( isalnum('c') == 0)
     printf("\nc is not a digit or a letter");
   else
     printf("\nc is a digit or a letter");

   if( isalnum('!') == 0)
     printf("\n! is not a digit or a letter");
   else
     printf("\n! is a digit or a letter");

   if( isalnum('2') == 0)
     printf("\n2 is not a digit or a letter");
   else
     printf("\n2 is a digit or a letter");

   return 0;
}

Output

Demonstration of isalnum():

A is a digit or a letter
c is a digit or a letter
! is not a digit or a letter
2 is a digit or a letter

Explanation
In this example, we have used if...else statements instead of a conditional operator.