isalnum() Function in C: Syntax and Examples

what-is-isalnum-in-c.jpg

The isalnum() function is a utility function in C that is used to determine whether the characters in a string are alphanumeric or not. In this blog, we will discuss the syntax of the isalnum() function, its use cases, and other character handling functions that are included in the <ctype.h> package.

Table of Contents:

Syntax of isalnum() in C

The syntax of the isalnum() function is quite simple, as it only takes one parameter.

int isalnum(int ch);

Parameters:

  • ch is the character you want to test. Even though it’s written as int, it’s typically just a char behind the scenes.

Return Value:

  • If the character is alphanumeric, a non-zero integer (true) is returned.
  • If the character is not a letter or digit, a 0 (false) is returned.

How isalnum() in C Works Internally

Let’s dig a little deeper and understand the logic behind how character handling functions like isalnum() determine whether a character is alphanumeric or not. Internally, the isalnum() function checks if the character falls within the ranges of alphabetic characters or numeric digits.

It typically behaves as if implemented like this:

int isalnum(int ch) {
return isalpha(ch) || isdigit(ch);
}

As you can see, the isalnum() function is a combination of the isalpha() and isdigit() functions. We will discuss these two functions in the coming sections. But first, let’s see how C reads these characters.

ASCII Value Check 

Characters in C are represented by using the ASCII values. Here is how isalnum() interprets the value that is passed as a parameter:

  • Digits (0–9): ASCII values 48 to 57
  • Uppercase Letters (A–Z): ASCII values 65 to 90
  • Lowercase Letters (a–z): ASCII values 97 to 122

If it matches any of those ranges, you’ll get a non-zero result. Otherwise? Zero.

Note: explicitly that these ranges are standard for ASCII-based systems, which is nearly universal in C environments, but not guaranteed across all platforms.

Required Header (<ctype.h>)

Before we can use the isalnum() function in our code, we need to add the <ctype.h> header so that the compiler knows where to find the function’s declaration. The <ctype.h> header file contains the declarations for character handling functions like isalnum(), isalpha(), isdigit().

#include <ctype.h>

Insert this into the top of your code, and on the lines after, you should be able to use the isalnum() function without the compiler throwing an error. 

Examples of isalnum() in C

The isalnum() function is a handy utility that can be used to solve many everyday programming problems. Let’s look at some real-world exercises where we can learn to implement the isalnum() function. 

1. Validating a Username Field

Imagine you want to make sure that a username input only contains letters or digits, the isalnum() function in C is the easiest way to solve this.

C

Output:

user_name_example

Try removing the “!” from the username to see the output change.

2. Basic Password Complexity Check

The isalnum() function can be used to make sure that there is at least one letter and one digit in a password. Websites and applications usually use this to make sure that users use a secure password.

C

Output: 

password_check

3. Cleaning Up a Title for a Filename

If a user gives you a messy title, you can clean it to make a safe filename.

C

Output:

filename

4. Filtering Characters in a Text Line

If you’re writing a code analyzer or a text processor, this is how you could filter out only the alphanumeric characters.

C

Output:

filtering_characters

Edge Cases and Limitations of isalnum() in C

While isalnum() is a handy tool for checking if characters are alphanumeric, it’s important to know its limitations to avoid unexpected results in your C programs. 

1. Non-ASCII or Unicode Characters

isalnum() does a great job with basic letters and numbers, but it has limits. It only works properly with standard characters (like A–Z, a–z, and 0–9). If you try using it with accented letters like é or ü, or characters from other languages, it might not behave the way you expect.

isalnum('é'); // May lead to undefined behavior on some systems

2. Special Characters Are Not Alphanumeric

Characters like @, #, %, *, and even spaces, tabs (\t), or newlines (\n) aren’t alphanumeric. That means isalnum() will return 0 for all of them. 

isalnum('@'); // Returns 0

isalnum(' '); // Returns 0

So if you’re using isalnum() to filter input, you might accidentally remove characters the user actually intended to include, like a space between names or an email symbol like @. That’s why it’s important to use isalnum() only when you specifically want to allow only letters and digits, and nothing else.

3. Negative Input Values (Except EOF)

If you give isalnum() a negative value (other than EOF), it can behave unpredictably, and that can cause bugs that are hard to track down. This usually happens when you’re working with characters outside the standard range. To avoid issues, it’s a good idea to cast your characters to unsigned char before using them with isalnum():

isalnum(-1); // Undefined behavior

Always make sure to cast char to unsigned char if there’s a risk of negative values:

isalnum((unsigned char)ch);

4. Misuse with Strings Instead of Characters

A common mistake for beginners is trying to pass a whole string to isalnum() instead of a single character. But isalnum() is meant to check one character at a time, not entire strings.

isalnum("A"); // Incorrect  (expects an int, not a string)

isalnum('A'); // Correct

The C Standard Library includes a bunch of useful character-handling functions in the <ctype.h> header, and many of them work well alongside isalnum().

Function Description
isalpha() Returns true if the character is an alphabet (A–Z or a–z)
isdigit() Returns true if it’s a digit (0–9)
isalnum() Returns true if it’s a letter or digit (alphanumeric)
ispunct() Checks if it’s a punctuation mark (like !, @, ,, etc.)
isspace() Checks for whitespace characters (space ‘ ‘, tab \t, newline \n, etc.)
isupper() Checks if it’s an uppercase letter
islower() Checks if it’s a lowercase letter
toupper() Converts a lowercase letter to uppercase
tolower() Converts an uppercase letter to lowercase

Conclusion

The isalnum() function can seem small, but it can be powerful when implemented well in your code. In this blog, we have learned the syntax of the isalnum() function, its edge cases, and even real-world examples. If you are looking to learn more about how to build your own applications using C, we highly recommend our project-led C programming certification course to get you a head start.

isalnum() in C – FAQs

1. What does the isalnum() function do in C?

The isalnum() function in C checks whether a character is either a letter (A to Z or a to z) or a digit (0 to 9). If it is, the function returns a non-zero value. If not, such as in the case of a space, symbol, or punctuation mark, it returns 0.
This function is handy when you want to validate input, remove special characters, or ensure that a string contains only letters and numbers.

2. Which header file is required for isalnum()?

To use the isalnum() function in your C program, make sure to include the header at the top:

#include <ctype.h>
3. Can I use isalnum() with strings in C?

No, isalnum() only works with single characters. If you want to check a full string, you’ll need to go through each character one at a time and apply isalnum() to it:

// Loop through the string and flag any non-alphanumeric characters
for (int i = 0; str[i] != '\0'; i++) {
    if (!isalnum((unsigned char)str[i])) {
        // Found something we don't want — maybe log or skip it
    }
}
4. What is the difference between isalnum(), isalpha(), and isdigit()?
  • isalnum() checks for both letters and digits.
  • isalpha() checks only for alphabetic letters (A–Z, a–z).
  • isdigit() checks only for numeric digits (0–9).

So, isalnum() is like a combination of isalpha() and isdigit().

5. What happens if I pass a negative value to isalnum()?

If you pass a negative value (other than EOF) to isalnum(), the result can be unpredictable. The C standard doesn’t define what should happen in that case, which means your code might behave strangely, crash, or appear to work depending on the system and compiler. To stay safe, cast the character to unsigned char before passing it:

isalnum((unsigned char)ch);

This ensures the value is within the correct range and avoids any undefined behavior.

About the Author

Senior Associate - Digital Marketing

Shailesh is a Senior Editor in Digital Marketing with a passion for storytelling. His expertise lies in crafting compelling brand stories; he blends his expertise in marketing with a love for words to captivate audiences worldwide. His projects focus on innovative digital marketing ideas with strategic thought and accuracy.

Advanced Data Science AI