Isalnum() function in C
Introduction:
The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not.
The alphanumeric number consists of the alphabet (a-z or A-Z) and a numeric value (0-9). This function uses <ctype.h> header file in the program.
The isalnum() function has zero value when the passed argument is not an alphabet or number. If the passed argument is alphanumeric, the function returns non-zero values.
For example, If we passed the alphabet 'a', then the isalnum() function returns non-zero values. Similarly, if we gave the numeric number '9', this function also returns the non-zero value. But when we passed neither an alphabet nor a numeric value, like '@', then this function returns zero.
Syntax of the isalnum() function in C language:
Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value. The syntax of the isalnum () function is:
int isalnum (int ch)
The isalnum() function checks whether the arguments are alphanumeric or non-alphanumeric. Here ch is taken as an argument or character.
Parameters of the isalnum() function in C language:
The main parameter of the isalnum() function is ch. Ch represents the given argument. Ch is checking whether the given value is alphanumeric or not an alphanumeric value.
Returns Value of the isalnum() function in C language:
The isalnum() function returns zero if the given number is not an alphanumeric value. The function returns a non-zero value if the given argument is an alphanumeric number. The alphanumeric numbers are 0 to 9, a to z and A to Z. The non alphanumeric values are @, #, %, $, etc.
Header file of the isalnum() function in C language :
Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension.
The isalnum() function checks whether the given argument is alphanumeric or non-alphanumeric. The primary use of the header file is to propagate the declaration of code files.The header file of the isalnum() function in the C language is <ctype.h>.
It writes in the program as #include <ctype.h>.
Example 1 –
Here we given a example of the isalnum() function using C language:
//header file initialize
#include <stdio.h>
#include <ctype.h>
//main code of this program
int main()
{
char c;
//print the result
printf(“Enter a character : ”);
scanf(“%c”, &c);
if (isalnum(c) == 0)
//print the result
printf (“The character is not an alphanumeric %c”,c);
else
printf (“The character is an alphanumeric %c”,c);
//end the program
return 0;
}
Output: We compile and run the above program. The result of this above program is:
Enter a character: h
The character is an alphanumeric h
Enter a character: @
The character is not an alphanumeric @
Enter a character: 3
The character is an alphanumeric 3
To better understand, we give another example of the isalnum() function.
Example 2 –
Here we given a example of the isalnum() function using C language:
//header file initialize
#include <stdio.h>
#include <ctype.h>
//main code of this program
int main()
{
char value1 = ‘P’;
char value2 = ‘5’;
char value3 = ‘#’;
if (isalnum(value1));
printf(“The character is an alphanumeric \n”);
else
printf(“The character is not an alphanumeric \n”);
if (isalnum(value2));
printf(“The character is an alphanumeric \n”);
else
printf(“The character is not an alphanumeric \n”);
if (isalnum(value3));
printf(“The character is an alphanumeric \n”);
else
printf(“The character is not an alphanumeric \n”);
//end the program
return 0;
}
Output: We compile and run the above program. The result of this above program is:
The character is an alphanumeric
The character is an alphanumeric
The character is not an alphanumeric
In the above two programs, we have used the isalnum () function; by the programs, we briefly discussed how to write any C language program using the isalnum () function. The isalnum () function uses to check whether the given character is alphanumeric or not.
We use <ctype.h> header file in this program. The two programs' output depends on whether the input value is alphanumeric or non-alphanumeric. We also share the result of the above two programs.