C语言中文字符

Python014

C语言中文字符,第1张

在C语言中,如果输出中文字符,可以通过printf函数直接输出。printf函数在内部提供这种机制,实现宽字符的转换,因此都可以正常输出,比如:

printf("中文测试\n")

另外也可以使用wprintf来输出存储在变量的中文字符,需要进行语言的区域设置。示例如下:示例如下,输出宽字符“中”字。

#include <stdio.h>

#include <locale.h>

int main()

{

setlocale(LC_ALL, "chs")

wchar_t wc = L'中'

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

return 0

}

需要准备的材料分别有:电脑、C语言编译器。

1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。

2、在test.cpp文件中,输入C语言代码:char a[] = "中文"puts(a) 。

3、编译器运行test.cpp文件,此时成功定义了中文并进行了输出。

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

}