C programming isalnum ctype.h

In C programming the <ctype> isalnum function check if the argument is alphanumeric.In other words this function check if the argument is Alphabetic characters or numeric value.Note which character is taken as Alphabetic character or numeric depends on the current locale set.More about how current locale influence this function is discuss in detail at the later section of this post.For the first part we will be dealing with the English Alphabets and the English numeric value.The declaration of the function is given below.

int isalnum(int c);

c -This is an integer value which is converted to it’s corresponding unsigned character.Check the ASCII chart for the integer value and their corresponding character.

Return type
int -This function returns true if the argument is character or digit.We can also say that isalpha() returns true for all the argument for which ‘isdigit’ and ‘isalpha’ functions return true.The return value may not be necessarily 1,for instance in English Alphabets system passing lower case character will return 2,upper case character will return 1 and passing integer will return 4.Again the return value vary according to the locale set,more about this is discuss later.

If you are passing the argument as character-‘a to z’ or ‘A to Z’,the function returns true.However,if you are passing an integer value as argument,only an integer within a specific set of integers value will return true.There are three sets,

i)Integer set [48 , 57],this set map to the integer [0,9] in ASCII chart
ii)Integer set [65 , 90],this set map to the upper case character [A,Z] in ASCII chart and
iii)Integer set [97 , 122],this set map to the lower case character [a,z] in ASCII chart.

Passing any other integer value besides from the above set will always return false,why? because any other integer value does not map to the English alphabets or English number.A code example is given below.

Code example

int ret;

ret = isalnum(‘C’);

printf(“ret=%d” , ret);

ret = isalnum(‘c’);

printf(“ret=%d” , ret);

/*Passing integer as argument */
ret = isalnum( 50 );

printf(“\n\nret=%d” , ret);

ret = isalnum( 80 ); //same as passing ‘P’

printf(“\nret=%d” , ret);

ret = isalnum( 100 ); //same as passing ‘d’

printf(“\nret=%d” , ret);

ret = isalnum( 60 );

printf(“\nret=%d” , ret);




The fourth output is false since 60 does not signify an alphabetic character or a numeric value.

Compiler specific behavior

Note there are some conditions under which different compiler will behave differently.For instance in Code::Blocks(using MinGw compiler) if an argument like ‘abg’ is passed the return value will depend on whether the last character is upper case or lower case,if upper case it returns 1,if lower case it returns 2.In Visual Studio however,you will get “Debug assertion failure”.

Another specific behavior you will see is when you pass an integer value.In Code::blocks,if you pass any integer value say ‘x’ not fulfilling the condition “x >= && x<255“,the function will return 0.However,in Visual Studio the function will pass “Debug assertion failure ..” message.It seems Visual Studio is more strict about which integer value can be agreed as an acceptable argument for ‘isalpha’ function.A code example is given below.

Code example

printf(“%d” , isalpha(‘abc’) );

printf(“\n%d” , isalpha(‘neW’) );

printf(“\n%d” , isalpha(1200) );

printf(“\n%d” , isalpha(-4) );

printf(“\n%d” , isalpha(-1) ); //Vs and CB retruns 0

Output in Code::blocks,


Except for the the last line of code in which the Visual Studio returns 0,the first four lines of code will pass “Debug assertion failure” is VS.

How current locale influence the behavior of ‘isalpha’ function

Suppose your current locale is set to other locale say to Turkey;the default locale is always set to English.If you pass a Turkish Alphabets ‘isalpha’ will return true since Turkish Alphabets is part of the Turkey locale.A specific locale can be set using the function ‘setlocale’ and passing first argument as “LC_CTYPE” and second argument as “trk_TUR.1254”.The first argument is simply a way to tell the compiler that ‘isalpha’ function should be affected by the locale set and the second argument is what actually set the locale to Turkey locale.If you want to set the Locale to other Language say Greek,pass the second argument as “ell_GRC.1253”,for more locale value visit the page https://lists.gnu.org/archive/html/guile-devel/2014-06/msg00027.html.Note the locale setting value provided here are only meant for MinGW compiler i.e. it works only in Code::blocks or Clion(if MinGW Compiler is used),it does not work in Visual Studio.Visual Studio has different value to set the locale.A code example is provided below.

Note include the header <locale.h> to use the function ‘setlocale’.

Code example

unsigned char c=’\0c7′ ; //The hexadecimal ‘\0c7’ represent the character ‘Ç’

if( isalpha(c) )
printf(“True:The character is part of the current locale.”);
printf(“False:The character is not part of the current locale.”);

/**Setting the locale to Turkey **/
setlocale( LC_CTYPE , “trk_TUR.1254 ” );

if( isalpha(c) )
printf(“\nTrue:The character is part of the current locale.”);
printf(“\nFalse:The character is not part of the current locale.”);

Output in CB,

False:The character is not part of the current locale.
True:The character is part of the current locale.

Related links

->C isdigit ctype.h

->C isalpha ctype.h

->C isxdigit

->C iscntrl