C programming isalpha ctype.h


In C programming the <ctype.h> isalpha function test if the argument is an Alphabetic character,meaning an upper case or lower case character.Which character is considered as Alphabetic character will depend on the specific locale set.If you set the locale to French only French Alphabet letters will be considered as Alphabetic character.We will discuss more about how to set specific locale and also see some examples when the locale is set to any locale other then English later in the post.First let us deal with “C” locale which is the default locale and also represent the English locale.The declaration of the function is shown below.

int isalpha(int c);

Parameters:
c -An integer or character to be tested for Alphabetic letter.

Return type
int -Return non-zero value only when ‘c’ is an Alphabetic character.What value is returned will on the locale set.In “C” locale for upper case character the returned value is 1 and for lower case the returned value is 2.

In “C” locale there are 26 lower case characters [a,z] and 26 upper case characters [A,B] for which the function will return non-zero value.If you are passing an integer value as an argument then only the integer within the set [65,90] -map to the upper case character [A,Z] in ASCII table- and [97,122]-map to the lower case [a,z] in ASCII table- will return non-zero value.For any other integer value the function always return false.And note in Visual Studio if you pass any integer x<-1 and x>255 the function will throw “Debug assertion failure” message.

Code example

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

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

printf(“\n\n%d” , isalpha( 74 ) ); //same as passing ‘J’

printf(“\n%d” , isalpha( 109 ) ); //same as passing ‘m’

printf(“\n\n%d” , isalpha( ‘&’ ) ); //return false since ‘&’ is not part of English Alphabet

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

printf(“\n\n%d” , isalpha( 300 ) ); //Visual Studio throws debug assertion failure here

Output,

2
1

1
2

0
0
0

The next section explains the behavior of ‘isalpha’ when the locale is set to any other locale besides the “C” (English) locale.



Setting specific locale

We can set to any specific locale by calling the function ‘setlocale’ define under the header <locale.h>.The first argument must always be ‘LC_CTYPE‘ macro and the second argument must be a string that set the locale to any language locale.For instance if you want to set the locale to German the second argument should be “deu_DEU.1252” or for Turkish locale “trk_TUR.1254“.Note “deu_DEU.1252” and “trk_TUR.1254” will work only in Code::blocks which uses MInGW compiler in,it will not work in Visual Studio.For VS refer to documentation of the VS compiler.A code example when the locale is set to German is given below.

Code example for Code::Blocks users

printf(“\n\n%d” , isalpha( ‘\xDF’ )); //’xDF’ hexadecimal represent the German small letter ‘ß’

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

printf(“\n%d” , isalpha( ‘\xC4’ )); //’xC4′ hexadecimal represent the German capital letter ‘Ä’

printf(“\n%d” , isalpha( ‘\x11F’ )); //’x11F’ hexadecimal represent the Turkish letter ‘ğ’ so here 0 will be returned

Output in CB,

258
257
257
0

Note here for German locale passing upper case letter return 257 and passing lower case return 258.

You can try out the program for other locale or for your own locale if you know which string is used to set the locale in ‘setlocale’.


Related links

->C isalnum ctype.h

->C isdigit ctype.h

->C isspace

->C isxdigit