C programming toupper ctype.h


The C <stype.h> toupper function returns the corresponding upper case(capital letter) of the lower case()small letter character passed.In different locale whether the character is considered as upper case or lower case will depend on the locale.In “C” locale we have the Latin Alphabet ‘a’ to ‘z’ as lower case and ‘A’ to ‘Z’ as upper case.The declaration of the function is given below.

int toupper(int c);

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

Return type
int -Return the upper case value of the lower case character passed.If upper case is passed as argument that character is returned.And note if the upper 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 upper case to avoid any unexpected behavior in your program.

Note if the character is not part of any Locale alphabet like (,:,?,etc, the character is returned unchanged.A code example is given below.It is evaluated in “C” locale;the default locale of the compiler.

Code example

unsigned int ret;

ret=toupper(‘q’);

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

ret=toupper(‘#’);

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

ret=toupper(‘\xE4’); //’xE4′ stands for German lower case ‘ä’

Output ,

ret=81
char=Q

ret=35
char=#

ret=-28
char=Σ

In the 3rd ‘toupper’ call the argument ‘\Xe4′(‘ä’) is passed but “C” does not support German Alphabet so the return value is unexpected.



Testing ‘toupper’ in specific locale

Suppose we want to get the upper case of the German Alphabet using ‘toupper’,first of all we will call the function ‘setlocale'(included in header locale.h) and pass the first argument as “LC_CTYPE” macro and the second argument as “deu_DEU.1252“.The second argument will be different for different locale.A code example is given below.

Code example

unsigned int ret;

setlocale(LC_CTYPE , “deu_DEU.1252”);

ret=toupper(”);

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

ret=toupper(‘\xE4’); //’xE4′ stands for German lower case ‘ä’

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

ret=toupper(‘\xDF’); //’xDF’ stands for German lower case ‘ß’

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

Output in Code::Blocks,

ret=69
char=E

ret=196
char=Ä

ret=223
char=ß

After setting to German local the corresponding upper case of ‘ä’ and ‘ß’ which are ‘Ä’ and ‘ß’ is returned respectively.


Related links

->C tolower ctype.h

->C isupper ctype.h

->C isalpha ctype.h

->C isprint ctype.h



[adcsh]