标签:style http color io os ar sp div c
宽字符:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <locale.h>
int main()
{
char str[10] = "轩辕";
printf("%d,%d\n", sizeof(str), strlen(str));//10,4
printf("%d\n", sizeof("轩辕")); //5
printf("%c%c\n", str[0], str[1]); //轩
printf("%c %c\n", str[0], str[1]); //乱码
- printf("%d,%d\n", sizeof("hello"), sizeof(L"hello"));// 6 12
char *p = "hello天朝";
wchar_t *pw = L"hello天朝";
printf("%s\n", p); //hello天朝
//宽字符的错误输出:
printf("%s\n", pw); //h (输出结果不对)
printf("%ls\n", pw); //hello (输出结果不对)
printf(L"%s\n", pw); //无输出
printf(L"%ls\n", pw); //无输出
//正确输出:
//1:必须包含locale.h 2:必须设置本地语言 3:ls是处理宽字符的
setlocale(LC_ALL, "zh-CN"); //简体中文
printf("%ls\n", pw); //hello天朝 (注意在setlocale下这句话也能正确打印)
wprintf(L"%ls\n", pw); //hello天朝
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main()
{
wchar_t wstr[10] = L"刘威";
printf("%d\n", sizeof(wstr));//20
printf("%d\n", strlen(wstr));//4
printf("%d\n", sizeof("刘威"));//5
printf("%d\n", strlen("刘威"));//4
printf("%d\n", sizeof(L"刘威"));//6
printf("%d\n", strlen(L"刘威"));//4
return 0;
}
标签:style http color io os ar sp div c
原文地址:http://www.cnblogs.com/l6241425/p/4001153.html