C isalnum() Function Tutorial

In this section, we will learn what the isalnum() function is and how to use it in C.

C isalnum() Function

The `isalnum()` function is used to check whether a character is an `alphabet` character or a `number`. (A-Z or a-z or 0-9)

The prototype of the function exists in the `ctype.h` header file and we need to include this header file in order to use `isalnum()` function.

isalnum() Function Syntax

Here’s the prototype of the `isalnum()` function:

int isalnum(int argument);

isalnum() Function Parameters

This function takes one argument and that is the character we want to check.

isalnum() Function Return Value

The return value of the function is:

  • 0: If the character was neither an `alphabet` nor a `number`.
  • Positive value: if the character was in fact either an `alphabet` or a `number`.

Example: using isalnum() function in C

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

int main() {

    char character = 'a';
    char c2 = '?';

    printf("The first character: %d \nThe second character: %d",isalnum(character),isalnum(c2));

    return  0;
}

Output:

The first character: 2

The second character: 0

The first character in this example is `a` and so the result of calling the `isalnum` function is a positive value because `a` is an alphabet character. But the result of the second character is 0 because the actual value of the character is `?` and it’s neither an `alphabet` nor a `number`.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies