setlocale(LC_ALL, "");">

c语言怎么写中文

来源:这里教程网 时间:2026-02-21 16:46:17 作者:

如何在 C 语言中输出中文

想要在 C 语言中输出中文,需要执行以下步骤:

    使用 UTF-8 编码
    C 语言默认使用 ASCII 字符编码,无法直接输出中文。因此,需要将源代码文件编码为 UTF-8。

    使用

    setlocale()
    函数设置语言环境
    setlocale()
    函数设置程序的语言环境。对于中文输出,需要使用以下代码:

    <code class="c">setlocale(LC_ALL, "");</code>

    使用

    printf()
    函数输出中文
    使用
    printf()
    函数输出中文时,需要在格式字符串中使用
    %s
    占位符,并以 UTF-8 编码的字符串作为参数。例如:

    立即学习“C语言免费学习笔记(深入)”;

    <code class="c">printf("你好,世界!\n");</code>

示例代码:

<code class="c">#include <stdio.h>
#include <locale.h>
int main() {
    // 设置语言环境
    setlocale(LC_ALL, "");
    // 输出中文
    printf("你好,世界!\n");
    return 0;
}</code>

相关推荐