我正在用C语言编写程序,希望在cmd.exe中运行时菜单中能够显示希腊字符。有人说为了包含希腊字符,必须使用类似于以下代码的printf
:
printf(charset:IS0-1089:uffe);
但是他们不确定。
有人知道如何做吗?
我正在用C语言编写程序,希望在cmd.exe中运行时菜单中能够显示希腊字符。有人说为了包含希腊字符,必须使用类似于以下代码的printf
:
printf(charset:IS0-1089:uffe);
但是他们不确定。
有人知道如何做吗?
此代码将打印γειά σου
:
#include "windows.h"
int main() {
SetConsoleOutputCP(1253); //"ANSI" Greek
printf("\xE3\xE5\xE9\xDC \xF3\xEF\xF5");
return 0;
}
γειά σου
编码为windows-1253时的结果。如果您使用将数据保存为windows-1253的编辑器,则可以使用文字代替。另一种选择是使用OEM 737(这确实是DOS编码),或使用Unicode。SetConsoleOutputCP
来设置控制台代码页,但您也可以在运行程序之前输入命令chcp 1253
。我认为这只有在您的控制台支持希腊语时才能正常工作。您想要做的可能是将字符映射到希腊语,但使用ASCII码。对于C#而言,与C相同的想法。
913至936 =大写希腊字母
945至968 =小写希腊字母
在Suite101上阅读更多信息:使用希腊字母和C#:创建C#应用程序时如何正确显示ASCII代码| Suite101.com link。
printf
像这样打印Unicode字符:
printf("\u0220\n");
这将打印出Ƞ一种方法是打印宽字符串。不幸的是,Windows需要一些非标准设置才能使其正常工作。此代码在#if
块内执行该设置。
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
/* This has been reported not to autodetect correctly on tdm-gcc. */
#ifndef MS_STDLIB_BUGS // Allow overriding the autodetection.
# if ( _WIN32 || _WIN64 )
# define MS_STDLIB_BUGS 1
# else
# define MS_STDLIB_BUGS 0
# endif
#endif
#if MS_STDLIB_BUGS
# include <io.h>
# include <fcntl.h>
#endif
void init_locale(void)
// Does magic so that wprintf() can work.
{
// Constant for fwide().
static const int wide_oriented = 1;
#if MS_STDLIB_BUGS
// Windows needs a little non-standard magic.
static const char locale_name[] = ".1200";
_setmode( _fileno(stdout), _O_WTEXT );
#else
// The correct locale name may vary by OS, e.g., "en_US.utf8".
static const char locale_name[] = "";
#endif
setlocale( LC_ALL, locale_name );
fwide( stdout, wide_oriented );
}
int main(void)
{
init_locale();
wprintf(L"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");
return EXIT_SUCCESS;
}
为了让旧版本的Visual Studio正常读取该文件,必须将其保存为带有BOM的UTF-8格式。您的控制台还必须设置为单间距Unicode字体(例如Lucida Console)才能正确显示它。为了在ASCII字符串中混合使用宽字符串,标准定义了%ls
和%lc
格式说明符用于printf()
,但我发现这些并不适用于所有情况。
另一种方法是将控制台设置为UTF-8模式(在Windows上,可以使用chcp 65001
来实现),然后使用printf(u8"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");
输出UTF-8字符串。在Windows系统中,UTF-8是二等公民,但通常可以正常工作。但是请尝试在不先设置代码页的情况下运行它,您会得到垃圾字符。