添加 ru_RU.CP1251 语言环境(在 debian 上取消注释 /etc/locale.gen
中的 ru_RU.CP1251
并运行 sudo locale-gen
)和
使用 gcc -fexec-charset=cp1251 test.c
编译以下程序(输入文件为 UTF-8)。结果是空的。只是字母'я'是错误的。
其他字母确定为小写或大写即可。
#include <locale.h>
#include <ctype.h>
#include <stdio.h>
int main (void)
{
setlocale(LC_ALL, "ru_RU.CP1251");
char c = 'я';
int i;
char z;
for (i = 7; i >= 0; i--) {
z = 1 << i;
if ((z & c) == z) printf("1"); else printf("0");
}
printf("\n");
if (islower(c))
printf("lowercase\n");
if (isupper(c))
printf("uppercase\n");
return 0;
}
为什么 islower()
和 isupper()
都不能处理字母 я
?
最佳答案
答案是 CP 1251 中该字符的小写版本的编码是十进制 255,而您的实现的 islower()
和 isupper()
不接受或返回该值(通常被解释为 EOF)。
您需要追踪运行时库的源代码以了解它的作用和原因。
解决方案是编写您自己的实现,或包装您拥有的实现。就个人而言,我从不直接使用这些函数,因为有很多陷阱。
关于c - 如何修复语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40298801/