宽字符串
宽字符:
#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,4printf("%d\n",sizeof("轩辕")); //5printf("%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天朝return0;}
宽字符 strlen sizeof :
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>int main(){wchar_t wstr[10]= L"刘威";printf("%d\n",sizeof(wstr));//20printf("%d\n", strlen(wstr));//4printf("%d\n",sizeof("刘威"));//5printf("%d\n", strlen("刘威"));//4printf("%d\n",sizeof(L"刘威"));//6printf("%d\n", strlen(L"刘威"));//4return0;}

浙公网安备 33010602011771号