C programing isupper ctype.h


In C programming the <ctype.h> isupper function test if the character is an upper case(Capital) character.Which character is considered as an upper case letter will depend on the current local setting.In “C” locale-the default locale of the compiler- all the Latin Alphabet capital letter are considered as upper case.The declaration of the function is given below.

int isupper(int c);

Parameters:
c -The character to check if it is an upper case.

Return type
int -Return non-zero value if the argument is upper case.

Note in “C” locale for each character the return value of ‘islower‘ and ‘isupper‘ functions are mutually exclusive.However,there may be some alphabetic characters in other locale that may return the same value by ‘islower’ and ‘isupper’ function.

Link: C islower

Code example

printf (“%d” , isupper(‘W’) );

printf (“\n%d” , isupper(75) );

printf (“\n%d” , isupper(‘c’) );

printf (“\n%d” , isupper(‘\xC4’) ); //passing German letter ‘Ä’

Output,

1
1
0
0

In the fourth line we have passed German capital letter ‘Ä’,but the locale is set to “C” locale and German Alphabet is not supported in this locale,hence the function return 0.

The next section discuss the behavior of ‘isupper’ when set to locale other than the “C” locale.





Setting German specific locale

If we set the locale to German only the upper case letter of German Alphabet will return true by ‘isupper’ function.To set the locale to German in MinGW compiler call the function ‘setlocale’ and pass the first argument as “LC_CTYPE” macro and pass the second argument as “deu_DEU.1252“.Consider the code below.

Code example

setlocale(LC_CTYPE , “deu_DEU.1252”);

printf (“%d” , isupper(‘\xC4’) );

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

printf (“\n%d” , isupper(‘\xE4’) );

printf (“\n%d” , isupper(‘c’) );

Output,

1
1
0
0

Now the function return true for ‘Ä’ and false for ‘ä’.Also since ‘A’ is part of German Alphabet the function return true and false for ‘c’.

Of you want to set the locale other than German you must call the function ‘setlocale’ and first argument as “LC_CTYPE” but the second argument will vary depending on the locale you want to set,for instance to set the locale to Turkish pass “trk_TUR.1254“.Read your compiler documentation to get your preferred valid locale string.


Related links

->C isspace

->C ispunct

->C iscntrl

->C isdigit

->C isalnum

->C isxdigit

->C toupper ctype.h