c - 如何在 C 中打印出特殊字符,如 å、ä、ö?
问题描述
例如,如果我尝试打印出“Ä”,我会得到这个字符:õ。我该如何解决?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main (){
char name[20];
printf("Enter first name: ");
scanf("%s", name);
if(strcmp(name, "Carl") == 0){
printf("Carl är bra!");
}
else{
printf("Kung!");
}
return 0;
}
(顺便说一句,我使用的是代码::块)
解决方案
在 Windows 上,将控制台模式设置为 UTF16 并使用wprintf
宽字符串文字而不是printf
.
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
_setmode(_fileno(stdout), _O_U16TEXT); // this is windows specific
wprintf(L"Carl är bra!");
return 0;
}
但是,正如我在关于在源代码中保留 unicode 字符的评论中提到的那样。\uNNNN
最好只使用转义序列内联 unicode 字符。使用如下打印语句。
wprintf(L"Carl \u00e4r bra!"); //0x00E4 is 'ä'
推荐阅读
- json - 将json读取到pandas DataFrame,但有数千行到pandas append
- sql - 同一记录中的 SQL Server 数据
- python - PyObject* 的“字符串”表示的生命周期
- c# - 如何在运行时显示闪烁的叠加层并再次将其关闭,由视图模型属性控制?
- azure-keyvault - 大厅与 azure key Vault 集成
- r - 在 dplyr 管道命令中应用函数
- excel - EXCEL在排序/使用公式后与单元格内容保持超链接
- php - PHP - 确保数组有一定数量的项目
- python - 从给定字符串中删除偶数的连续重复字符
- azure - 如何获取 Azure RM 资源的创建日期,包括来自 Azure 的所有资源