c语言怎样才能输出中文???(最简单的方法)

Python010

c语言怎样才能输出中文???(最简单的方法),第1张

定义一个字符串变量,在这个变量的值中就可以输入中文了。汉字是多字节的,一个char放不下,可以使用字符数组,但需要给数组分配空间,或者使用string。

例如:

#include<stdio.h>

int main(void)

{

char a[128]

printf("请输入所需输出的汉字:")

scanf("%s",a)

printf("%s\n",a)

return 0

用C语言显示中文,其实重点并不是编程

而是系统编码格式

以及系统输出的编码

如果二者相同,

那么在写代码的时候直接打汉字,存成字符串输出即可。

如果不同,

就需要做转码。

比如

编码用utf-8

输出用gbk等。

由于很难在运行时判断系统编码,所以不建议C语言使用中文输出,很容易出现乱码。

1、中文字符串可以使用printf()、puts()等函数直接输出。

#include <stdio.h>

#include <locale.h>

int main()

{

const char str[] = "这里全是中文"

printf("\n输出字符数:%d\n", printf(str))

puts(str)

return 0

}2、单个中文字符,需要进行本地化设置,需要使用宽字符版的printf()即wprintf输出。

#include <stdio.h>

#include <locale.h>

int main()

{

setlocale(LC_ALL, "chs")

wchar_t wc = L'中'

wprintf(L"%c\n",wc)

return 0

}