How to test locale in C? All my tests fail.

Artem Kuchin matrix at itlegion.ru
Thu May 24 12:14:26 UTC 2007


Oliver Fromme wrote:
> Artem Kuchin wrote:
>> I have a very stupid problem. I cannot convert to upper
>> or lower case using manually set locale (setlocale(..)).
>> 
>> A very simple program:
>> [...]
>>         printf("IS UPPER ?: %d\n",isupper('?'));
>>         printf("IS UPPER ?: %d\n",isupper('?'));
>>         printf("IS LOWER ?: %d\n",islower('?'));
> That's a common pitfall.  Chars are signed by default on
> FreeBSD, and the isupper() etc. function take an int type
> argument.  That means that characters >= 128 end up as
> negative numbers, so they fail all isupper() and islower()
> checks, and toupper()/tolower() don't touch them at all.
> 
> The solution is to typecast the constants to unsigned char
> explicitly, like this:  isupper((unsigned char) '?') etc.
> Your program will work fine then.

My stupid. I should've noticed that. Thank you very much!
Issue closed.

--
Regards,
Artem
 



More information about the freebsd-stable mailing list