C Function Isalnum
## C Library Function - `isalnum()`
The `isalnum()` function is a built-in utility in the C standard library used to check whether a given character is alphanumeric. In other words, it verifies if a character is either a decimal digit (`0`β`9`) or an uppercase/lowercase letter (`A`β`Z`, `a`β`z`).
This function is declared in the `` header file.
---
## Syntax and Declaration
```c
#include
int isalnum(int c);
```
### Parameters
* **`c`**: This is the character to be checked, passed as an `int`.
* The value of `c` must be representable as an `unsigned char`, or it must be equal to the macro `EOF` (End of File).
* Passing values outside this range results in undefined behavior.
### Return Value
* **Non-zero value (True)**: If the character `c` is an alphanumeric character (either a letter or a digit).
* **Zero (False)**: If the character `c` is not alphanumeric (such as punctuation, spaces, control characters, or special symbols).
---
## How It Works (Locale Dependency)
By default, in the standard `"C"` locale, `isalnum()` is equivalent to:
* `isalpha(c)` (checks for `A`β`Z` or `a`β`z`) OR `isdigit(c)` (checks for `0`β`9`).
In other locales, additional characters may be classified as alphanumeric depending on the system's character set and localization settings.
---
## Code Example
The following program demonstrates how to use the `isalnum()` function to check different types of characters.
```c
#include
#include
int main()
{
int var1 = 'd'; // Lowercase letter
int var2 = '2'; // Digit
int var3 = '\t'; // Tab character (control character)
int var4 = ' '; // Space character
// Check var1
if( isalnum(var1) )
{
printf("var1 = |%c| is alphanumeric\n", var1);
}
else
{
printf("var1 = |%c| is NOT alphanumeric\n", var1);
}
// Check var2
if( isalnum(var2) )
{
printf("var2 = |%c| is alphanumeric\n", var2);
}
else
{
printf("var2 = |%c| is NOT alphanumeric\n", var2);
}
// Check var3
if( isalnum(var3) )
{
printf("var3 = |\\t| is alphanumeric\n");
}
else
{
printf("var3 = |\\t| is NOT alphanumeric\n");
}
// Check var4
if( isalnum(var4) )
{
printf("var4 = |%c| is alphanumeric\n", var4);
}
else
{
printf("var4 = |%c| is NOT alphanumeric\n", var4);
}
return 0;
}
```
### Output
When you compile and run the program above, it produces the following output:
```text
var1 = |d| is alphanumeric
var2 = |2| is alphanumeric
var3 = |\t| is NOT alphanumeric
var4 = | | is NOT alphanumeric
```
---
## Important Considerations
### 1. Safe Type Casting
Because `isalnum()` accepts an `int` but expects the value to be representable as an `unsigned char` (or `EOF`), passing a standard `char` directly can lead to undefined behavior on platforms where `char` is signed (e.g., when processing non-ASCII characters with negative values).
To ensure safety, always cast the character argument to `unsigned char`:
```c
char ch = 'A';
if (isalnum((unsigned char)ch)) {
// Safe to execute
}
```
### 2. Return Value is Not Guaranteed to be `1`
When `isalnum()` evaluates to true, it returns a **non-zero** value, which is not necessarily `1`. When writing conditional logic, always check against `0` implicitly or explicitly:
```c
// Recommended
if (isalnum(c)) { ... }
// Avoid this
if (isalnum(c) == 1) { ... } // This might fail on some implementations
```
YouTip