C programing tolower ctype.h


The C <stype.h> tolower function returns the corresponding lower case of the upper case character passed.Which character is considered as upper case and which lower case depend on the current locale setting.In “C” locale the 26 Latin alphabet ‘A’ to ‘Z’ are considered upper case and their corresponding letter ‘a’ to ‘z’ are considered as lower case.The declaration of the function is given below.

int tolower(int c);

Parameters:
c -The character to get it’s corresponding lower case.

Return type
int -Return the lower case value of the upper case character passed.If lower case is passed as argument that character is returned.And note if the lower case character of the argument passed does not exist the function may return some unspecified value,make sure to verify that the return value is the correct lower case to avoid any unexpected behavior in your program.

In “C” locale the return value is obvious if the character passed is between ‘A’ to ‘Z’,which is ‘a’ to ‘z’.If you pass a punctuation character like %,^,*,etc, that same character is return.If you pass some other character belonging to different locale the return value is unexpected.Consider the example below.

Code example

unsigned int ret;

ret=tolower(‘A’);

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

ret=tolower(‘m’);

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

ret=tolower(‘&’);

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

ret=tolower(‘\xD6’); //’xD6′ stands for Turkish upper case ‘Ö’

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

Output,

ret=97
char=a

ret=109
char=m

ret=38
char=&

ret=17462
char=6

In the 4th line a Turkish character ‘\xD6′(‘Ö’) is passed but “C” locale does not support Turkish Alphabet so the return value is some unexpected value.

The next section test the ‘tolower’ function is Turkish locale.





Testing ‘tolower’ in specific locale

In this section let us see if the locale is set to Turkish will the function ‘tolower’ return it’s corresponding lower case character.To set the locale to Turkish call the function ‘setlocale’ and pass the first argument as “LC_CTYPE” locale and the second argument as “trk_TUR.1254“.The second argument will vary for different locale.

Code example :include the header locale.h to access ‘setlocale’

unsigned int ret;

setlocale(LC_CTYPE , “trk_TUR.1254”);

unsigned int ret;

ret=tolower(‘K’);

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

ret=tolower(‘\xD6’); //’xD6′ stands for Tukish upper case letter ‘Ö’

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

ret=tolower(‘\xDC’); //’xDc’ stands for Tukish upper case letter ‘Ü’

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

Output in Code::blocks,

ret=107
char=k

ret=246
char=ö

ret=252
char=ü

In the first ‘tolower’ call ‘k’ is return since ‘K’ is it’s upper case in Turkish Alphabet.

In the second ‘tolower’ call ‘\xD6′(‘Ö’) is passed hence it lower case ‘ö’ is return.

In the third ‘tolower’ call ‘\xDC'(‘Ü’) is passed hence it lower case ‘ü’ is return.

Conclusion,’tolower’ seems to work fine in Turkish locale.


Related links

->C toupper ctype.h

->C islower ctype.h

->C ispunct ctype.h

->C isxdigit ctype.h