To use printf
,并假设您使用的是美国本地化的 Windows,控制台代码页为 437(运行chcp
检查),那么如果将源文件保存在代码页 437 中,则以下更正后的代码将起作用。一种方法是使用 Notepad++ 并设置Encoding->Character sets->Western European->OEM-US
在菜单上。这样做的缺点是你的源代码在大多数编辑器中都不能很好地显示,除非它们特别支持 cp437,甚至 Notepad++ 在重新打开文件时如果不再次设置编码也无法正确显示它。
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
int main()
{
char pos[9] = {'X','O','X','O','X','O','X','O','X'};
printf(" %c ║ %c ║ %c \n", pos[0], pos[1], pos[2]);
printf("═══╬═══╬═══\n");
printf(" %c ║ %c ║ %c \n", pos[3], pos[4], pos[5]);
printf("═══╬═══╬═══\n");
printf(" %c ║ %c ║ %c \n", pos[6], pos[7], pos[8]);
system("pause"); system("pause");
}
在 Windows 上,由于 API 本身是 UTF-16,因此更好的方法是使用以下代码并将文件保存为带 BOM 的 UTF-8:
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
int main()
{
char pos[9] = {'X','O','X','O','X','O','X','O','X'};
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L" %C ║ %C ║ %C \n", pos[0], pos[1], pos[2]);
wprintf(L"═══╬═══╬═══\n");
wprintf(L" %C ║ %C ║ %C \n", pos[3], pos[4], pos[5]);
wprintf(L"═══╬═══╬═══\n");
wprintf(L" %C ║ %C ║ %C \n", pos[6], pos[7], pos[8]);
system("pause");
}
输出(两种情况):
X ║ O ║ X
═══╬═══╬═══
O ║ X ║ O
═══╬═══╬═══
X ║ O ║ X
Press any key to continue . . .