isalnum() function in C

This section will discuss the isalnum() function in the C programming language to check whether the passed character as the argument is a valid alphanumeric character or not. The isalnum() function is declared in the ctype.h header file. The isalnum() function takes one argument and tests for both digits and the character of the alphabet. If the given character is either the digits (0 - 9) or alphabets (a - z or A to Z), it returns a true or non-zero value. Otherwise, it returns false or zero.

For example, if the given character is 'a', the isalnum() function returns a non-zero value. Similarly, we enter 5 as the numeric to the isalnum() function that returns a non-zero value. If the given number is neither the alphabets (a to z or A to Z) letter nor the digits from 0 to 9, it returns zero.

isalnum() function in C

Syntax of the isalnum() function

Following is the syntax of the isalnum() function in the C programming languages, as follows:

Here, the isalnum function takes ch as the argument to check the given argument is alphanumeric or not.

Parameters:

ch: It represents the alphabets or numeric numbers to be checked.

Return Value:

It returns a non-zero value when the passed 'ch' character is a digit or a letter. Otherwise, it shows 0.

Example 1: Program to demonstrate the usage of the isalnum() function in C

Let's consider an example to check the given character is an alphanumeric in the C programming language.

Output:

'A' is an alphanumeric character.
'e' is an alphanumeric character.
'$' is not an alphabetic or numeric character.
'7' is an alphanumeric character.
 ' ' is not an alphabetic or numeric character.
'0' is an alphanumeric character.

In the above program, the isalnum() function checks each variables' values whether the passed character is an alphanumeric value or not. If the given character is alphanumeric, it returns the %c is an alphanumeric character; else, it shows the %c is not an alphanumeric character.

Example 2: Program to input a character and check the alphanumeric using the isalnum() function

Let's consider an example to input the character from the user and check whether the given character is alphanumeric in the C programming language.

Output:

Enter a valid character: t
't' is a valid alphanumeric character.

Here, we input the 't' character from the user and then use the isalnum() function to check the given character is alphanumeric.

2nd execution:

Similarly, we input 7 from the user and check the given number is numeric or not. If the number contains 0 to 9 digits, the isalnum () function returns that the given number is numeric.

3rd execution:

Here, we input the '+" character and use the isalnum() function to check the alphanumeric character. And we get the given character is not alphanumeric means neither the character is alphabets nor digits from 0 to 9.

Example 3: Program to return non-zeroes values for alphanumeric characters in C

Let's consider an example to validate the alphanumeric character and returns a non-zero values using the isalnum() function in the C programming language.

Output:

The result of valid uppercase alphanumeric is passed: 1
The result of valid lowercase alphanumeric is passed: 2
The result of valid alphanumeric character is passed: 4
The result of valid alphanumeric character is passed: 4
The result of non-alphanumeric character is passed: 0

Example 4: Program to check all alphanumeric character of the array using the isalnum() function

Let's consider an example to check all array character is alphanumeric or not using the isalnum() function in the C programming language

Output:

'#' is NOT an alphabet or numeric character.
'&' is NOT an alphabet or numeric character.
'@' is NOT an alphabet or numeric character.
'9' is an Alphanumeric character.
'Z' is an Alphanumeric character.
'0' is an Alphanumeric character.
'!' is NOT an alphabet or numeric character.