How to Print Chinese in C Language
In C language, the printf function can be used to output Chinese strings. It is important to note that Chinese characters are stored in Unicode encoding in the program, so the wide character type wchar_t is needed to represent Chinese characters.
Here is an example program demonstrating how to output Chinese strings.
#include <stdio.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, ""); // 设置本地化环境,支持输出中文字符
wchar_t str[] = L"你好,世界!"; // 使用宽字符类型表示中文字符串
wprintf(L"%ls\n", str); // 使用wprintf输出中文字符串
return 0;
}
To correctly display Chinese characters, it is necessary to use a localization environment that supports Chinese characters. This can be set through the setlocale function. On Windows systems, it can be set to setlocale(LC_ALL, “”) to use the system’s default localization environment; on Linux systems, it can be set to setlocale(LC_ALL, “zh_CN.UTF-8”) to use the “zh_CN.UTF-8” localization environment.
Additionally, the wprintf function is used to output wide-character strings, with the format specifier %ls being used specifically for wide-character strings. It is important to note that wide-character strings should have an L prefix to indicate their type.